hdoj--1258--Sum It Up(dfs)
Sum It Up
Total Submission(s): 5534 Accepted Submission(s): 2890
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.
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.
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.
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
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
题意:从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)的更多相关文章
- 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 ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- 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) ...
- hdu 1258 Sum It Up (dfs+路径记录)
pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
- POJ 1564 Sum It Up(DFS)
Sum It Up Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- hdoj Max Sum Plus Plus(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:----最大M子段和问题给定由 n个整数(可能为负整数)组成的序列a1,a2,a3,……, ...
- 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 ...
- Sum It Up---poj1564(dfs)
题目链接:http://poj.org/problem?id=1564 给出m个数,求出和为n的组合方式:并按从大到小的顺序输出: 简单的dfs但是看了代码才会: #include <cstdi ...
- 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 ...
随机推荐
- Cannot find module 'crc'
这个时候你只需要打开你nodejs安装的目录,在其中执行 npm install crc(这里查什么模块(module)就安装什么模块).
- http接口 两种调用http接口的方法
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; ...
- SQLite 在 Android 的应用
Android提供了创建和使用SQLite数据库的API(Application Programming Interface,应用程序编程接口). 在Android系统中,主要由类SQLiteData ...
- Verification之PSL之intro
1 PSL - Property specification language 1.1 Property - Characteristics of the designs/verification e ...
- C# --MVC实现简单上传下载
首先创建一个默认的控制器Defaultcontroller 然后生成视图View 在视图里面 创建文件选择器 创建上传.下载按钮 代码如下 <body> <div> <f ...
- Graph network classification(As a beginner, continue to update)
Data arrangement 1.Reference Webs http://nlp.csai.tsinghua.edu.cn/~tcc/ https://blog.csdn.net/a60964 ...
- 用Python来实现斐波那契数列.
1).递归 def fib_recur(n): assert n >= 0, "n > 0" if n <= 1: return n return fib_rec ...
- json简介及josn数组中取字符
1.json字符串就是字符串,只不过格式是Json格式的,以键值对的形式存在,键和值可以是字符串,数字,空值,数组等. json对象在花括号中书写,一个json对象包含多个键值对,json对象以花括号 ...
- VCSA服务重启命令
Sphere Web Client界面的服务分别是: vmware-mbcs vmware-netdumper vmware-rbd-watchdog 分别执行命令确认,首先执行命令: service ...
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd
Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...