(Hdu step 4.3.3) Sum It Up (select m numbers from n numbers for them to reach the specified and targetSum, and output all valid sequences), hdutargetsum

Source: Internet
Author: User

(Hdu step 4.3.3) Sum It Up (select m numbers from n numbers for them to reach the specified and targetSum, and output all valid sequences), hdutargetsum

Question:

Sum It Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 140 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 that add up to t. for example, if t = 4, n = 6, and the list is [,], then there are four different sums that equal 4: + + 2, and 2 + 1 + 1. (A number can be used within a sum as every 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 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 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 (random SIVE ), 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.
 
OutputFor each test case, first output a line ining 'sums', the total, and a colon. then output each 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 may be repeated in the sum as usual 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 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's fourth college Program Design Competition
RecommendJGShining


Question Analysis:

Simple question. Deep Search. This is a one-dimensional deep search question. Therefore, you do not need to create a two-dimensional map [] [] for recording map information.

One-dimensional nums [] records the original sequence. In addition, we usually use the sort () function to sort data. It seems that there is no problem, but this question uses the qsort () function.


The Code is as follows:

/** D. cpp ** Created on: February 24, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 101; int n; // The number of elements in the original sequence int targetSum; // The number of elements in the target and bool flag; // whether int cnt can be properly solved; // The number of elements in the result sequence int value [maxn]; // result sequence int nums [maxn]; // original sequence/*** comparison function. * learn this writing method */int cmp (const void * a, const void * B) {return (* (int *) a <* (int *) B ); // In this case, the search is performed from large to small. * index: The index currently traversed * sum: The current result If the sum of the sequence and */void dfs (int index, int sum) {if (sum> targetSum) {// if the sum of the current result sequence is> specified and return; // if this condition does not meet the requirements, return .} if (sum = targetSum) {// if the sum of the current result sequence is specified and flag = true; // flag, indicates that the search is successful // printf ("Sums of % d: \ n", targetSum); // each value in the output result sequence is 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 last search start point for (I = index; I <n; ++ I) {// traverses each element in the sequence and uses each element as the search result. Search for if (nums [I]! = Last) {// if the current search start point is different from the previous one. (the sequence produced by the first 2 and the following 1 in the 2 1 sequence is considered to be the same sequence as that produced by the second 2 and the following 1) value [cnt ++] = nums [I]; // set the current element as the result sequence last = nums [I]; // update the Search Start dfs (I + 1, sum + nums [I]); // continue deep search from the next index .. cnt --; // roll back the number of elements in the result sequence} int main () {while (scanf ("% d", & targetSum, & n )! = EOF, n) {printf ("Sums of % d: \ n", targetSum); int I; for (I = 0; I <n; ++ I) {scanf ("% d", & nums [I]);}/*** qsort () function 4 each parameter has the following meanings: * nums: array header address * n: number of elements in the array * third parameter: size of each element * Fourth parameter: Comparison function */qsort (nums, n, sizeof (nums [0]), cmp); cnt = 0; flag = false; dfs (0, 0); if (flag = false) {printf ("NONE \ n") ;}} 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.