标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1080 Accepted Submission(s): 742
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 using namespace std; 5 char str[17]; 6 int val[17][17]; 7 int len; 8 int ans; 9 10 void cal() 11 { 12 memset(val,0,sizeof(val)); 13 for(int i = 0 ; i < len ; i++) 14 for(int j = i ; j < len ; j++) 15 for(int k = i ; k <= j ; k++) 16 val[i][j] = val[i][j]*10+(str[k]-‘0‘); 17 } 18 void dfsr(int lsum,int pos, int rsum) 19 { 20 if(pos>=len) 21 { 22 if(rsum==lsum) 23 ans++; 24 return ; 25 } 26 for(int k = pos+1 ; k <= len ; k++) 27 dfsr(lsum,k,rsum+val[pos][k-1]); 28 } 29 void dfsl(int equ , int pos , int lsum) 30 { 31 if(pos>=equ) 32 dfsr(lsum,equ,0); 33 for(int k = pos+1 ; k <= equ ; k++) 34 dfsl(equ,k,lsum+val[pos][k-1]); 35 } 36 37 int main() 38 { 39 while(~scanf("%s",str)&&str[0]!=‘E‘) 40 { 41 ans = 0 ; 42 len = strlen(str); 43 cal(); 44 int equ; 45 for(equ = 1 ; equ < len ; equ++) 46 dfsl(equ,0,0); 47 printf("%d\n",ans); 48 } 49 return 0; 50 }
数位:
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 using namespace std; 5 #define N 17 6 char a[N]; 7 int equ; 8 int len; 9 bool ck(int sum) 10 { 11 int l= 0 , r=0; 12 int cur = 0; 13 for(int i = 0 ; i <= equ ;i++) 14 { 15 if(sum&(1<<i)) { 16 cur = cur * 10 + a[i]-‘0‘; 17 l+=cur; 18 cur = 0; 19 } 20 else cur = cur*10+a[i]-‘0‘; 21 } 22 if(cur != 0) l+=cur; 23 cur = 0 ; 24 for(int i = equ+1 ; i< len ; i++) 25 { 26 if(sum&(1<<i)){ 27 cur = cur * 10 + a[i]-‘0‘; 28 r+=cur; 29 cur = 0; 30 } 31 else cur = cur*10+a[i]-‘0‘; 32 } 33 if(cur!=0) r += cur; 34 if(l==r) return true; 35 else return false; 36 } 37 int main() 38 { 39 while(~scanf("%s",a)&&a[0]!=‘E‘) 40 { 41 int ans = 0 ; 42 len = strlen(a); 43 for( equ = 0 ; equ < len-1 ; equ++) 44 { 45 int tm = 1<<(len-1); 46 for(int j = 0 ; j < tm ;j++) 47 { 48 if(ck(j)){ 49 ans++; 50 // printf("%d %d\n", equ, j); 51 } 52 } 53 } 54 printf("%d\n",ans>>1); 55 } 56 return 0; 57 }
A very hard Aoshu problem(dfs或者数位)
标签:
原文地址:http://www.cnblogs.com/shanyr/p/4750337.html