(hdu step 4.3.3)Sum It Up(從n個數中選出m個數讓他們的和達到指定和targetSum,輸出所有的合法序列),hdutargetsum

來源:互聯網
上載者:User

(hdu step 4.3.3)Sum It Up(從n個數中選出m個數讓他們的和達到指定和targetSum,輸出所有的合法序列),hdutargetsum

題目:

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 [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.
 
InputThe 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 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(inclusive), 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 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 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浙江工業大學第四屆大學生程式設計競賽 
RecommendJGShining


題目分析:

               簡單題。深搜。這還是一道一維的深搜題。所以不需要開二維的用於記錄地圖資訊的map[][],只需要開一個

一維的nums[]記錄原始序列即可。還有就是之前一般都是用sort()這個函數來實現排序,好像也沒有什麼問題.但是這道題用了一下qsort()這個函數


代碼如下:

/* * d.cpp * *  Created on: 2015年2月24日 *      Author: Administrator */#include <iostream>#include <cstdio>using namespace std;const int maxn = 101;int n;//原始序列的元素個數int targetSum;//目標和bool flag;//是否有合適解int cnt;//結果序列的元素個數int value[maxn];//結果序列int nums[maxn];//原始序列/** * 比較函數. * 學習這種寫法 */int cmp(const void* a, const void* b) {return (*(int*) a < *(int*) b);//這個時候是從大到小排列}/** * 深搜. * index:目前遍曆到的索引 * sum: 目前結果序列的和 */void dfs(int index, int sum) {if (sum > targetSum) {//如果目前結果序列的和已經>指定和return;//這種情況不符合要求,返回.}if (sum == targetSum) {//如果目前結果序列的和就是指定和flag = true;//標記flag,表示尋找成功//printf("Sums of %d:\n",targetSum);//輸出結果序列中的每一個值int i;for (i = 0; i < cnt - 1; ++i) {printf("%d+", value[i]);}printf("%d\n", value[cnt - 1]);}int i;int last = -1;//用於標記上一次的搜尋起點for (i = index; i < n; ++i) {//遍曆序列中的每一個元素,分別以每一個元素作為搜尋起點進行搜尋if (nums[i] != last) {//如果當前的搜尋起點和上一次的搜尋起點不一樣.(2 2 1 1 這個序列中第一個2 和 後面的1產生的序列與第二個2與後面的1產生的序列認為是同一個序列)value[cnt++] = nums[i];//將當前元素假如結果序列last = nums[i];//更新搜尋起點dfs(i + 1, sum + nums[i]);//從下一個索引開始繼續深搜..cnt--;//將結果序列的元素個數復原}}}int main() {while (scanf("%d%d", &targetSum, &n) != EOF, n) {printf("Sums of %d:\n", targetSum);int i;for (i = 0; i < n; ++i) {scanf("%d", &nums[i]);}/** * qsort()函數的4各參數的含義如下: * nums:數組首地址 * n:數組的元素個數 * 第三個參數:每一個元素的大小 * 第四個參數:比較函數 */qsort(nums, n, sizeof(nums[0]), cmp);cnt = 0;flag = false;dfs(0, 0);if(flag == false){printf("NONE\n");}}return 0;}







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.