Sum It up Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3695 Accepted Submission (s): 1873
Problem 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 was [4,3,2,2,1,1], then there was 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 would contain one or more test cases, one per line. Each test case is contains T, the total, followed by N, and 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 be is a positive integer less than, n would be an integer between 1 and (inclusive), and X1,..., xn wi ll be positive integers less than 100. All numbers is separated by exactly one space. The numbers in each list appear in nonincreasing order, and there could be repetitions.
Output for each test case, first output a line containing ' Sums of ', the total, and a colon. Then output the sum, one per line; If there is no sums, output the line ' NONE '. The numbers within each sum must appear in nonincreasing order. A number is repeated in the sum as many times as it is repeated in the original list. The sums themselves must is sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must is sorted by their first number; Sums with the same first number must is sorted by their second number; Sums with the same first and numbers must is sorted by their third number; And so on. Within each test case, all sums must is distince; The same sum connot 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+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
Test instructions Nothing special, it's easy to read. And I won't say much.
Difficulty: This question to judge whether to repeat, duplicate of the formula only once also must ensure that Dfs starts the starting point is different.
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace
Std
const int M = 1000 + 5;
int n, m; int num[m]; Data input int ans[m]; Data output int vist;
Mark int step;
BOOL Flag;
void Dfs (int x, int cnt) {if (cnt>n) return;
else if (cnt==n)//Meet the requirements of the output {flag=true;
printf ("%d", ans[1]);
for (int i=2; i<step; i++) printf ("+%d", Ans[i]);
printf ("\ n");
return; } int vist=-1; Used to determine if the starting start of Dfs is the same for (int j=x+1; j<=m; J + +) {if (num[j]!=vist) {ans[step++]=num[j
];
VIST=NUM[J];
DFS (J, Cnt+num[j]);
step--;
}}} int main () {while (scanf ("%d%d", &n, &m) &&n &&m) {flag=false;
Step=1; memset (num, 0, sizeof (num));
Data initialization memset (ans, 0, sizeof (ans)); for (int i=1; i<=m; i++) scanf ("%d", &num[i]);
printf ("Sums of%d:\n", N);
DFS (0, 0);
if (!flag) printf ("none\n");
} return 0;
}