Sum It Up
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 3
Input
The input file will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x 1 , . . . , x n . If n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and x 1 , . . . , x n will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
Output
For each test case, first output a line containing `Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line `NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distinct; the same sum cannot appear twice.
Sample Input
4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25
25
0 0
Sample Output
Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of
400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <map> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 int n,m,ans,pos,len,c,out; 7 int flag; 8 const int maxn=110; 9 int a[maxn],vis[maxn]; 10 int sum[maxn]; 11 void dfs(int m) 12 { 13 if(m>n) 14 return ; 15 if(ans>c) 16 return ; 17 if(ans==c) 18 { 19 flag=1; 20 for(int j=0;j<m-1;j++) 21 { 22 printf("%d+",sum[j]); 23 } 24 printf("%d ",sum[m-1]); 25 return ; 26 } 27 for(int i=pos;i<n;i++) 28 { 29 if(vis[i]==0) 30 { 31 if(out==a[i]) 32 continue; 33 vis[i]=1; 34 sum[m]=a[i]; 35 ans+=sum[m]; 36 pos=i; 37 dfs(m+1); 38 39 vis[i]=0; 40 ans-=sum[m]; 41 out=sum[m]; 42 } 43 } 44 } 45 void init() 46 { 47 memset(vis,0,sizeof(vis)); 48 out=ans=pos=flag=m=0; 49 } 50 bool cmp(const int x,const int y) 51 { 52 return x>y; 53 } 54 void solve() 55 { 56 printf("Sums of %d: ",c); 57 dfs(0); 58 59 if(!flag) 60 printf("NONE "); 61 } 62 int main() 63 { 64 while(scanf("%d%d",&c,&n)) 65 { 66 if(n==0&&c==0) 67 break; 68 init(); 69 for(int i=0;i<n;i++) 70 { 71 scanf("%d",&a[i]); 72 } 73 sort(a,a+n,cmp); 74 75 solve(); 76 } 77 return 0; 78 }
一个WA的奇怪代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <map> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 int n,m,ans,pos,len,c,out; 7 int flag; 8 const int maxn=110; 9 int a[maxn],vis[maxn]; 10 int sum[maxn]; 11 void dfs(int m,int len) 12 { 13 if(m>n) 14 return ; 15 if(ans>c) 16 return ; 17 if(m==len&&ans==c) 18 { 19 // printf("666 "); 20 flag=1; 21 for(int j=0;j<m-1;j++) 22 { 23 printf("%d+",sum[j]); 24 } 25 printf("%d ",sum[m-1]); 26 return ; 27 } 28 for(int i=pos;i<n;i++) 29 { 30 if(vis[i]==0) 31 { 32 if(out==a[i]) 33 continue; 34 vis[i]=1; 35 sum[m]=a[i]; 36 ans+=sum[m]; 37 pos=i; 38 dfs(m+1,len); 39 40 vis[i]=0; 41 ans-=sum[m]; 42 out=sum[m]; 43 } 44 } 45 } 46 void init() 47 { 48 memset(vis,0,sizeof(vis)); 49 out=ans=pos=flag=m=0; 50 } 51 bool cmp(const int x,const int y) 52 { 53 return x>y; 54 } 55 void solve() 56 { 57 printf("Sums of %d: ",c); 58 for(int i=1;i<=n;i++) 59 { 60 pos=0; 61 out=0; 62 dfs(0,i); 63 } 64 65 if(!flag) 66 printf("NONE "); 67 } 68 int main() 69 { 70 while(scanf("%d%d",&c,&n)) 71 { 72 if(n==0&&c==0) 73 break; 74 init(); 75 for(int i=0;i<n;i++) 76 { 77 scanf("%d",&a[i]); 78 } 79 sort(a,a+n,cmp); 80 81 solve(); 82 } 83 return 0; 84 }