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 ...
随机推荐
- SQL Server之存储过程
存储过程的概念 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行. 存储过程中可以包含逻辑控制语句和数据操纵语句,它 ...
- Python批量添加库搜索路径
被win10 给坑了,换回Win7. 重装系统后,继续使用Python,Eclipse不用重装,pydev不用重装,只需重装Python2.7.6 X64 for win即可.然后,默认已安装的Pyt ...
- OpenCV的AdaptiveThreshold函数
摘自于OpenCV Doc2.410,opencv2refman文档. 1.函数原型 adaptiveThreshold //Applies an adaptive threshold to an a ...
- 个人作业—Alpha测试
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/homework/3338 这个作业要求在哪里 htt ...
- C# 去掉代码前边空格(格式化代码)
private void button1_Click(object sender, EventArgs e) { textBox2.Text = ""; string str = ...
- IDEA中使用Database管理工具
以下内容来自我的知乎回答IntelliJ IDEA中有什么让你相见恨晚的技巧? 说个冷门的,用IDEA操作数据库. 可能大部分不知道,IDEA是自带数据库管理工具的,类似于一个小型Navicat. 具 ...
- GETDATE()
定义和用法 GETDATE() 函数从 SQL Server 返回当前的时间和日期. 语法 GETDATE() 实例 例子 1 使用下面的 SELECT 语句: SELECT GETDATE() AS ...
- ES6中的Symbol
---恢复内容开始--- Symbol 1.1 概述 保证每个属性的名字都是独一无二的就好了,这样就从根本上防止属性名的冲突.这就是 ES6 引入Symbol的原因 在es6之前,JavaScript ...
- 数据结构总结(UPDATING......)
目标: 1.栈........√ 2.队列......√ 3.堆.........× 4.并查集...× 栈: #define MAXN 65536 struct stack{ int sz[MAXN ...
- CPA-IBE
1.Transaction ID 生成机制 在有的情况下,我们需要得到固定格式的序列号,而不是数据库默认的自增序列号, 1.1 通常方式(隐式生成并通过触发器实时插入相关表) 例如我们要求此序列号必须 ...