题目:
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 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 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. |
Input The 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. |
Output For 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 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+2 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 |
Source 浙江工业大学第四届大学生程序设计竞赛 |
Recommend JGShining |
题目分析:
简单题。深搜。这还是一道一维的深搜题。所以不需要开二维的用于记录地图信息的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; }
(hdu step 4.3.3)Sum It Up(从n个数中选出m个数让他们的和达到指定和targetSum,输出所有的合法序列)
原文:http://blog.csdn.net/hjd_love_zzt/article/details/43926239