Sum It Up

/*浙江工业大学1100
Sum It Up
Time Limit:1000MS  Memory Limit:32768K


Description:
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.

Input:
The input 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 x1, ..., xn. 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 x1, ..., xn 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
结果:Wrong answear,不知道为什么
*/

 1#include <iostream>
 2using namespace std;
 3
 4int Num[13];
 5int x[13= {0};
 6int old[13= {0};
 7int cw = 0;
 8int t,n;
 9bool f;
10
11void Backtrack(int l)
12{
13    if(l>=n){//到达叶结点
14        if(cw == t){
15            f = true;
16            int tmp[13];
17            int cnt = 0;
18            for(int i=0;i<n;i++){
19                if(x[i] == 1){
20                    tmp[cnt++= Num[i];
21                }

22            }

23
24            bool flag = false;
25            for(int i=0;i<cnt;i++){
26                if(tmp[i]!=old[i]){
27                    flag = true;
28                    break;
29                }

30            }

31            if(flag){
32                printf("%d",tmp[0]);
33                old[0= tmp[0];
34                for(int i=1;i<cnt;i++){
35                    printf("+%d",tmp[i]);
36                    old[i] = tmp[i];
37                }

38                printf("\n");
39            }

40        }

41        return;
42    }
else{//搜索子树
43        if(cw+Num[l]<=t){
44            x[l] = 1;
45            cw+=Num[l];
46            Backtrack(l+1);
47            cw-=Num[l];
48            x[l] = 0;
49        }

50        Backtrack(l+1);//x[l] = 0
51    }

52
53}

54
55int main()
56{
57    while(scanf("%d%d",&t,&n),n){
58        cw = 0;
59        for(int i=0;i<n;i++){
60            scanf("%d",&Num[i]);
61            x[i] = 0;
62            old[i] =-1;
63        }

64        printf("Sums of %d:\n",t);
65
66        f = false;
67        Backtrack(0);
68        
69        if(f == false){
70            printf("NONE\n");
71        }

72    }

73    return 0;
74}

希望各位给予指导,另外,附上一个能够AC的程序,供参考

 1#include <iostream>
 2using namespace std;
 3
 4#define N 15
 5
 6int t,n,a[N];
 7bool flag,used[N]= {0};
 8
 9void dfs(int cur,int sum)
10{
11    if(sum==t)
12    {
13        flag=false;
14        bool first=true;
15        for(int i=0; i<n; i++)
16            if(used[i])
17            {
18                if(first)
19                {
20                    printf("%d",a[i]);
21                    first=false;
22                }

23                else printf("+%d",a[i]);
24            }

25        printf("\n");
26    }

27    else
28        for(int i=cur; i<n; i++)
29        {
30            if(sum+a[i]>t) continue;
31            //  if( i && a[i]==a[i-1] && !used[i-1]) continue; //师兄的mark模板
32            if(!used[i])
33            {
34                used[i]=true;
35                dfs(i+1,sum+a[i]);
36                used[i]=false;
37
38                int k=i+1;
39                while(k<&& a[k]==a[i]) k++;
40                i=k-1;
41            }

42        }

43}

44
45int main()
46{
47    while(scanf("%d %d",&t,&n),n)
48    {
49        int i;
50        for(i=0; i<n; i++)
51            scanf("%d",&a[i]);
52        for(i=0; i<n; i++)
53            if(a[i]<=t) break;
54//  memset(used,false,sizeof(used));
55        flag=true;
56        printf("Sums of %d:\n",t);
57        dfs(i,0);
58        if(flag) printf("NONE\n");
59    }

60    return 0;
61}



 

posted on 2012-05-07 12:48 DGQKing 阅读(385) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年6月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜