Shredding Company(dfs)
http://poj.org/problem?id=1416
题意:将一个数分成几部分,使其分割的各个数的和最大并且小于所给的数。
凌乱了。。参考的会神的代码。。orz...
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <string.h> 3 4 int arr[102],ans[102]; 5 int max,len,f; 6 int a,b; 7 8 void dfs(int n,int now,int sum,int k,int t) 9 { 10 int m = n % 10; 11 if (!n) 12 { 13 arr[k] = now; 14 if (sum + now > a) 15 return ; 16 if (sum + now ==max) 17 f++; 18 if (sum +now > max) 19 { 20 max = sum + now; 21 f = 1; 22 len = k; 23 for (int i = 1; i <= k; i ++) 24 { 25 ans[i] = arr[i]; 26 } 27 28 } 29 return ; 30 } 31 dfs(n/10,now+t*m,sum,k,t*10); 32 arr[k] = now; 33 dfs(n/10,m,sum+now,k+1,10); 34 } 35 int main() 36 { 37 while(~scanf("%d%d",&a,&b)&&a&&b) 38 { 39 max = 0; 40 f = 0; 41 dfs(b/10,b%10,0,1,10); 42 if (max==0) 43 { 44 printf("error\n"); 45 continue; 46 } 47 if (f > 1) 48 { 49 printf("rejected\n"); 50 continue; 51 } 52 printf("%d",max); 53 for (int i = len; i >= 1; i --) 54 { 55 printf(" %d",ans[i]); 56 } 57 printf("\n"); 58 } 59 return 0; 60 }