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的更多相关文章

  1. hdu1258 Sum It Up (DFS)

    Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...

  2. 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 ...

  3. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  8. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

随机推荐

  1. hdu_4787_GRE Words Revenge(在线AC自动机)

    题目链接:hdu_4787_GRE Words Revenge 题意: 总共有n个操作,2种操作.每行读入一个字符串. 1.如果字符串以+开头,此为单词(即模式串,不考虑重复) 2.如果字符串以?开头 ...

  2. OpenCV2.x自学笔记——固定阈值

    threshold( const CvArr* src,  CvArr* dst,  double threshold,  double max_value,  int threshold_type) ...

  3. Servlet图片上传

    package com.servlet; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io ...

  4. [ An Ac a Day ^_^ ] hrbust 2291 Help C5 分形

    开博客这么久从来没写过自己学校oj的题解 今天写一篇吧 嘿嘿 原题链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProble ...

  5. digitalocean vpn安装配置教程

    digitalocean是美国一家专业的vps提供商,优势是性价比高,最低配置512MB内存vps每月只要5美元,导致大陆用户疯狂涌入.关于digitalocean申请方法.digitalocean速 ...

  6. pc app 桌面打包

    进入 http://nwjs.io/  下载 创建web项目,在项目根目录 创建文件package.json并填写 1 2 3 4 5 6 7 {   "name": " ...

  7. 洛谷-烤鸡-BOSS战-入门综合练习1

    题目背景 Background 猪猪hanke得到了一只鸡  题目描述 Description 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10 ...

  8. PHP ServerPush (推送) 技术

    用来代替ajax的请求 转自:http://blog.163.com/bailin_li/blog/static/17449017920124811524364/ 需求: 我想做个会员站内通知的功能. ...

  9. Python in minute

    Python 性能优化相关专题:    https://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/   Python wikipedi ...

  10. win安装Theano

    艰辛的安装Theano过程,把其中遇到的问题记录下来,三台机子都尝试了安装Theao,系统分别为:A机:win7 64-bit(笔记本).B机:win7 64-bit(台式机).C机:win8 64- ...