HDU1258 Sum it up
Sum it up
题意:给定一个数sum,和n个数,求sum可以由这n个数里面的那几个数的和表示。
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.
注意:输入,输出要求比较高,另外不能有重复。
递归的时候应该及时退出。
http://acm.hdu.edu.cn/showproblem.php?pid=1258
#include<cstdio>
#include<iostream>
int sum,n;
int flag=0;
int a[20],ans[20];
void dfs(int sums,int cut,int x)//sums代表当前的和,cut代表ans里的[1,cut),x代表a中的第x个数
{
if(sums==sum){
for(int i=1;i<cut;i++){
flag=1;
if(i==cut-1)
printf("%d\n",ans[i]);
else
printf("%d+",ans[i]);
}
return ;
}//如果结果sums==sum按格式输出ans 并且flag=1;
int t=-1;
for(int i=x;i<=n;i++){
if(t!=a[i]){
ans[cut]=a[i];
t=a[i];//避免重复
dfs(sums+a[i],cut+1,i+1);
}
}//
return ;//递归要有结束条件,不能是return;
}
int main ()
{
while(scanf("%d %d",&sum,&n),n||sum)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
flag=0;
printf("Sums of %d:\n",sum);
dfs(0,1,1);
if(flag==0)
printf("NONE\n");
}
return 0;
}
HDU1258 Sum it up的更多相关文章
- hdu1258 Sum It Up (DFS)
Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...
- 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 ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
随机推荐
- Inno setup 中 执行参数传递注意的地方
Inno setup编译器编译使用pascal脚本编写的打包代码,其中Run段可以执行某些特定的程序,遇到一个bat批处理文件传递参数的问题,记录如下 1: [Run] 2: Filename: &q ...
- Sharepoint 的定制 - 代码附加内容编辑器
//来源:http://www.cnblogs.com/codingcow/articles/69143.html Sharepoint是一种非常新而且很有用的技术. 现在微软把基本的功能集成到了Wi ...
- 移动端touch事件获取clientX, clientY
目有个交互需要实现手指滑动的交互,pc端使用mousedown,mousemove,mouseup监听实现. 但在ios设备上mousemove是不好监听的,同类的方法是touchstart,touc ...
- io外挂
c++里最快的io方式是什么呢? 详见这里. 同时给出一个比较常用的方式,就是用fread.然后自己解析文本,而不是用cin或者scanf,见这里: //fast io test #include & ...
- 《剑指Offer》算法题——“旋转数组”的最小数字
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减序列的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...
- [转载]C#控制台应用程序里调用自己写的函数的方法
(2011-08-15 15:52:13) 转载▼ 标签: 转载 分类: 技术类 原文地址:C#控制台应用程序里调用自己写的函数的方法作者:萧儿 最近写程序,遇到了一个很白痴的问题,记录下来,免得下次 ...
- zen cart global $db 这噶哒
zen cart $db变量是mysql数据库类的对象,是一个全局变量.在zen cart系统里面的任何一个地方都可以使用这个对象来查询数据库获取数据库的内容.只要需要用到查询数据库,都可以使用这个对 ...
- LeetCode OJ 219.Contains Duplicate 2
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- MT4 做指标模版
//+------------------------------------------------------------------+ //| guo.mq4 | //| Copyright 2 ...
- centos 6.2安装bind 9.8.2 master、slave与自动修改后更新
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dl528888.blog.51cto.com/2382721/1249311 随 ...