pojShredding Company
http://poj.org/problem?id=1416
1 #include<cstdio> 2 #include<cstring> 3 #define MAXN 100 4 using namespace std; 5 6 int c,m; 7 int ans[MAXN],t[MAXN]; 8 int max,r; 9 int ansk; 10 11 void dfs(int n,int sum,int now,int k,int p) 12 { 13 if(n==0) 14 { 15 t[k]=now; 16 if(sum+now>m) return; 17 if(sum+now==max) r++; 18 else if(sum+now>max) 19 { 20 max=sum+now; 21 r=1; 22 ansk=k; 23 for(int i=1; i<=k; i++) ans[i]=t[i]; 24 } 25 return ; 26 } 27 int mm=n%10; 28 dfs(n/10,sum,now+p*mm,k,p*10); 29 t[k]=now; 30 dfs(n/10,sum+now,mm,k+1,10); 31 } 32 int main() 33 { 34 while(scanf("%d%d",&m,&c)&&c&&m) 35 { 36 max=0,r=0; 37 dfs(c/10,0,c%10,1,10); 38 if(max==0) 39 { 40 printf("error\n"); 41 continue; 42 } 43 if(r>1) {printf("rejected\n");continue;} 44 printf("%d",max); 45 for(int i=ansk; i>=1; i--) 46 printf(" %d",ans[i]); 47 printf("\n"); 48 } 49 return 0; 50 }