1206#POJ1456 sum it up

来源:互联网 发布:js中new 关键字 编辑:程序博客网 时间:2024/05/23 01:13



摘要
-给出长度为n的有序的数列,求选择其中的部分数的和为给定t的方案,并输出每种组合方案 
原题目摘要
-sum it up 来源: http://poj.org/problem?id=1564

-

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 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:43+12+22+1+1Sums of 5:NONESums of 400:50+50+50+50+50+50+25+25+25+2550+50+50+50+50+25+25+25+25+25+25



题目理解
-给定的数据是非递增的,输出也是非递增的,刚好就可以顺着弄;就是dfs,findpath(start,total)为从start位数开始向后面找组合的和为total,递归findpath(start+1,total-Arr[start])和findpath(start+1,total);有点像背包,二分的去找; 
注意
-去重的话刚开始以为相同的输出 应该是相邻的,于是记录最后一组输出作为比较来去重,后面发现这样遇到相同的数很多时就会出问题;后面想了一下,重复都是因为不要某个数,而后面又出现这个数造成的,于是findpath(start,total)的时候想法在当前数后面连续的数都跳过,到下一个不相同的继续findpath(start+i,total);
日期
-2017 12 6
附加
-
代码
-

#include <algorithm>#include <cstdio>#include <iostream>#include <list>#define MAX 15using namespace std;int n,t;int Arr[MAX];list<int> ans;list<int>::iterator ia;bool none = true;void list_show(list<int> l){    ia = l.begin();    for(;ia!=l.end();ia++){        if(ia!=l.begin())putchar('+');            printf("%d",*ia);    }    putchar('\n');}void findpath(int start,int total){//从start起 凑total    if(total<0) return;    if(total==0){        none = false;        list_show(ans);        return;    }    if(start>=n) return;    ans.push_back(Arr[start]);    findpath(start+1,total-Arr[start]);    ans.pop_back();    for(int i=start+1,last = start;i<n;i++){//去重操作        if(Arr[last]!=Arr[i]){            findpath(i,total);            break;        }    }}void solve(int tt,int nn){    for(int i=0;i<nn;i++)scanf("%d",&Arr[i]);    printf("Sums of %d:\n",tt);    none = true;    findpath(0,tt);    if(none)printf("NONE\n");    ans.clear();}int main(){    //freopen("in","r",stdin);   while(scanf("%d%d",&t,&n)){        if(t==0&&n==0) break;        solve(t,n);    }    return 0;}


原创粉丝点击