http://poj.org/problem?id=1416
乱七八糟的改,乱七八糟的错,最后过了样例 1A了 。对于每个分割开的数来说 有几种组合方式 搜下去
View Code
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define INF 10000000 5 using namespace std; 6 int n,m,num[10],sum,g,vis[10],di,tt,mi,f; 7 char oo[10][10],o[10][10]; 8 void dfs(int x,int sum,int v) 9 { 10 int i,j,flag=0,y=1,ss,k; 11 char si[10]; 12 if(x>g) 13 { 14 if(n-sum<mi) 15 { 16 f = 0; 17 mi = n-sum; 18 for(i = v-1 ; i>=1 ; i--) 19 strcpy(oo[i],o[i]); 20 tt = v-1; 21 } 22 else 23 if(n-sum==mi) 24 { 25 if(f) 26 return ; 27 if(v-1!=tt) 28 { 29 f = 1; 30 return; 31 } 32 for(i = 1; i < v ; i++) 33 if(strcmp(oo[i],o[i])!=0) 34 { 35 f = 1; 36 break; 37 } 38 } 39 return ; 40 } 41 for(i = 0; i < g ; i++) 42 { 43 ss = 0; 44 y = 1; 45 k = 0; 46 for(j = x ; j <= i+x&&j<=g ; j++) 47 { 48 ss+=y*num[j]; 49 si[k++] = num[j]+'0'; 50 y = y*10; 51 } 52 if(ss+sum>n) 53 break; 54 y = j; 55 for(j = k-1 ; j>=0 ; j--) 56 o[v][k-1-j] = si[j]; 57 o[v][k] = '\0'; 58 dfs(y,ss+sum,v+1); 59 } 60 } 61 int main() 62 { 63 int i,j,k; 64 while(cin>>n>>m) 65 { 66 if(n==0&&m==0) 67 break; 68 memset(oo,0,sizeof(oo)); 69 mi = INF; 70 if(n==m) 71 { 72 cout<<n<<" "<<m<<endl; 73 continue; 74 } 75 g =0 ;sum=0;k=0;di=0;f=0; 76 while(m) 77 { 78 g++; 79 num[g] = m%10; 80 m = m/10; 81 } 82 for(i = 1; i <= g ; i++) 83 sum+=num[i]; 84 if(sum>n) 85 { 86 puts("error"); 87 continue; 88 } 89 sum = 0; 90 dfs(1,0,1); 91 if(f) 92 puts("rejected"); 93 else 94 { 95 cout<<n-mi<<" "; 96 for(i = tt; i > 1 ; i--) 97 cout<<oo[i]<<" "; 98 cout<<oo[1]<<endl; 99 } 100 } 101 return 0; 102 }