Sum It Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5534    Accepted Submission(s): 2890

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
浙江工业大学第四届大学生程序设计竞赛



题意:从m个数中取若干个数,是的这若干个数的和为n,输出每一个方案

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[100],n,m,a[100];
int ans;
void dfs(int pre,int x,int r)
{
if(pre==n)
{
ans++;
printf("%d",a[0]);
for(int i=1;i<r;i++)
printf("+%d",a[i]);
printf("\n");
}
else
{
for(int i=x;i<m;i++)
{
if(pre+num[i]<=n)
{
a[r]=num[i];
dfs(pre+num[i],i+1,r+1);
while(i+1<m&&num[i]==num[i+1]) //去重
i++;
}
}
}
}
int cmp(int a,int b)
{
return a>b;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==0&&n==0) break;
memset(num,0,sizeof(num));
memset(a,0,sizeof(a));
for(int i=0;i<m;i++)
scanf("%d",&num[i]);
sort(num,num+m,cmp);
printf("Sums of %d:\n",n);
ans=0;
dfs(0,0,0);
if(ans==0)
printf("NONE\n");
}
return 0;
}

hdoj--1258--Sum It Up(dfs)的更多相关文章

  1. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  2. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  3. HDU 1258 Sum It Up(dfs 巧妙去重)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...

  4. hdu 1258 Sum It Up (dfs+路径记录)

    pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  6. POJ 1564 Sum It Up(DFS)

    Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  7. hdoj Max Sum Plus Plus(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:----最大M子段和问题给定由 n个整数(可能为负整数)组成的序列a1,a2,a3,……, ...

  8. HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏

    Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...

  9. Sum It Up---poj1564(dfs)

    题目链接:http://poj.org/problem?id=1564 给出m个数,求出和为n的组合方式:并按从大到小的顺序输出: 简单的dfs但是看了代码才会: #include <cstdi ...

  10. CodeForces 489C Given Length and Sum of Digits... (dfs)

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

随机推荐

  1. Windows 下MySQL zip 安装

    主要步骤: 1.下载解压到安装的文件夹 2.配置环境路径 3.配置my.ini文件,设置程序路径和数据存储路径 4.以管理员身份启动Mysqld install(提示sevice安装成功) 5.启动M ...

  2. OpenCV的AdaptiveThreshold函数

    摘自于OpenCV Doc2.410,opencv2refman文档. 1.函数原型 adaptiveThreshold //Applies an adaptive threshold to an a ...

  3. javascript中的计算题

    一.js中的数据类型共六种: 值类型五种:Boolea   Number  String  Null  undefined 引用类型:Object ----三大引用类型:Object   Array  ...

  4. case when用法小结

    case 对比字段 when 值 then 输出结果 when 值 then 输出结果 ....else 输出结果 end 对比字段可以不在case后面确定 可以把条件直接写在when后面,如果对比字 ...

  5. Selenium3+python 加载Firefox配置

    有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile_di ...

  6. S-HR快速查看shr日志

    http://localhost:6888/shr/appData.do?method=getApplicationLog&logFile=apusic.log.0&instance= ...

  7. [置顶] 来自 Google 的高可用架构理念与实践

    转自:   https://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=402738153&idx=1&sn=af5e76aad ...

  8. react 点击事件+父子传值

    接下来要做的效果是,在父组件添加两个按钮,点击后改变父组件传过去的值 父组件 import React, { Component } from 'react'; import Test from '. ...

  9. Mongodb--切片

    1.在3台服务器上分别运行 2717 , 27018,27109,互为副本集,形成3套replSet 2.在3台服务器上各配置config.server,运行在27020端口上,和配置mongod的命 ...

  10. Java 代理学习笔记

    http://blog.csdn.net/mr_seaturtle_/article/details/52686516