Sum it up HDU 1258

Source: Internet
Author: User
Document directory
  • Problem description
  • Input
  • Output
  • Sample Input
  • Sample output
  • Source
Sum it uptime limit: 2000/1000 ms (Java/other) memory limit: 65536/32768 K (Java/other) total submission (s): 1 accepted submission (s): 1 Font: times New Roman | verdana | georgiafont size: Regular → problem descriptiongiven 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, 1, 1], then there are four different sums
That equal 4: 4, 3 + 1, 2, and 2 + 1 + 1. (A number can be used within a sum
When times as it appears in the list, and a single number counts as
Sum.) Your job is to solve this problem in general. inputthe input will contain in one or more test cases, one per line. Each test
Case contains t, the total, followed by N, the number of integers in
List, followed by N integers X1,..., xn. If n = 0 it signals the end
The input; otherwise, t will be a positive integer less than 1000, n
Will be an integer between 1 and 12 (random SIVE), and X1,..., xn will be
Positive integers less than 100. All numbers will be separated
Exactly one space. The numbers in each list appear in nonincreasing
Order, and there may be repetitions. outputfor each test case, first output a line ining 'sums of ',
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 every 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
The same first number must be sorted by their second number; sums
The same first two numbers must be sorted by their third number; and so
On. Within each test case, all sums must be distince; the same sum
Connot appear twice. sample input
4 6 4 3 2 2 1 15 3 2 1 1400 12 50 50 50 50 50 50 25 25 25 25 25 250 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
Source

The second write. New feeling .. The key is to understand recursion...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int A[1000];
int hash[1000];
int N, M, flag = 0;

void debug( )
{
#ifdef P
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
}


void DFS(int x, int sum, int n)
{
int i, j;
if (sum == N)
{
if (flag == 0) {
printf("Sums of %d:\n", N);
flag = 1;
}
printf("%d",hash[0]);
for (j = 1; j < n; j++)
printf("+%d", hash[j]);
puts("");
return;
}
else {
for (i = x; i < M; i++) {
if ( A[i] > N)
continue;
else
{
//puts("**");
if (sum + A[i] > N)
continue;
if (x < i && A[i] == A[i -1])
continue;
hash[n] = A[i]; //4 6 4 3 2 2 1 1
DFS(i + 1, sum + A[i], n + 1);
}
}
}
}

int main( )
{

int i, j;
debug( );
while (scanf("%d%d", &N, &M) , N || M)
{
flag = 0;
for (i = 0; i < M; i++)
scanf("%d",&A[i]);
memset(hash, 0, sizeof(hash));
DFS( 0, 0 , 0);
if (!flag) {
printf("Sums of %d:\n",N);
puts("NONE");
}

}
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.