(Hdu step 4.3.3) Sum It Up (select the number of M from N to let them and reach the specified and Targetsum, outputting all the legal sequences)

Source: Internet
Author: User

Topic:

Sum It up
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): Accepted submission (s): 73
problem Descriptiongiven a specified total T and a List of n integers, find all distinct sums using numbers from the list. 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.
 
inputthe 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.
 
outputfor 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 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 Zhejiang University of Technology fourth session of college students Program design contest
Recommendjgshining


Topic Analysis:

Simple question. Deep Search. This is still a one-dimensional deep search problem. So there is no need to open two-dimensional map[][to record the map information], only need to open a

One-dimensional nums[] records the original sequence. There is a sort () function that was used to achieve sorting, as if there were no problems. But the problem used a bit qsort () this function


The code is as follows:

/* * d.cpp * * Created on:2015 February 24 * author:administrator * * #include <iostream> #include <cstdio>usin G namespace Std;const int maxn = 101;int n;//The number of elements of the original sequence int targetsum;//target and bool flag;//whether there is a suitable solution int cnt;//The number of elements of the result sequence int value[m axn];//the result sequence int nums[maxn];//The original sequence/** * comparison function. * Learn this notation */int CMP (const void* A, const void* b) {return (* (int*) A < * (int*) b);//This time it is from large to small arrangement}/** * deep search. * Index: The currently traversed index * sum: The current result sequence and */void dfs (int index, int sum) {if (sum > Targetsum) {///if the current result sequence and has been > specified and return;//this The situation does not meet the requirements, return.} if (sum = = targetsum) {//) if the sum of the current result sequence is the designation and flag = true;//Flag Flag, indicates the lookup succeeds//printf ("Sums of%d:\n", targetsum);// Each value in the output sequence is an int i;for (i = 0; i < cnt-1; ++i) {printf ("%d+", Value[i]);} printf ("%d\n", Value[cnt-1]);} int I;int last = -1;//used to mark the previous search starting point for (i = index; i < n; ++i) {//iterate through each element in the sequence, searching for each element as the search starting point, if (nums[i]! = last) {// If the current search starting point is not the same as the previous search starting point. (2 2 1 1 The sequence of the first 2 and subsequent 1 in this series is considered to be the same sequence as the second 2 and the sequence produced by the subsequent 1) value[cnt++] = nums[i];//The current element if the result sequence last = nums[i];//Update the search starting pointDFS (i + 1, sum + nums[i]);//Continue deep search from the next index. cnt--;//rolls back the number of elements of the result sequence}}}int main () {while (scanf ("%d%d", &targetsum, &n)! = EOF, N) {printf ("Sums of%d:\n", Target Sum), int i;for (i = 0; i < n; ++i) {scanf ("%d", &nums[i]);} /** * Qsort () The 4 parameters of the function are as follows: * Nums: The first address of the array * N: The number of elements of the array * Third parameter: The size of each element * The fourth parameter: comparison function */qsort (nums, N, sizeof (Nums[0]), CMP); c NT = 0;flag = False;dfs (0, 0), if (flag = = False) {printf ("none\n");}} return 0;}







(Hdu step 4.3.3) Sum It Up (select the number of M from N to let them and reach the specified and Targetsum, outputting all the legal sequences)

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.