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. Java 泛型 泛型的约束与局限性

    Java 泛型 泛型的约束与局限性 @author ixenos 不能用基本类型实例化类型参数 不能用类型参数代替基本类型:例如,没有Pair<double>,只有Pair<Doub ...

  2. 浙大pat1042题解

    1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...

  3. strpos 返回0时 ,比较false 不能加单引号

    $a =  'a.approve'; $num = strpos($a,'a.admin'); if(strpos($a,'a.approve') !== 'false'){ //不能加单引号.变字符 ...

  4. 7.hibernat实现双向一对多(多对一)

    1.创建如下项目结构 2.在项目的src下创建hibernate.cfg.xml主配置文件 <?xml version="1.0" encoding="UTF-8& ...

  5. 单尺度二维离散小波重构(逆变换)idwt2

    clc,clear all,close all; load woman; %单尺度二维离散小波分解.分解小波函数haar [cA,cH,cV,cD]=dwt2(X,'haar'); %单尺度二维离散小 ...

  6. Qt5 OpenGL框架

    #ifndef MYRENDERER_H #define MYRENDERER_H #include <QOpenGLContext> #include <QOpenGLFuncti ...

  7. SQL Server 把当前日期中月份和几号中的0 去掉

    select left(convert(varchar(10),getdate(),20),4)+replace(right(convert(varchar(10),getdate(),20),6), ...

  8. Keyboard Test Utility v1.0.1.0 电脑键盘测试软件绿色版

    软件名称: 电脑键盘测试软件绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: 917KB图片预览: 软件简介:Keyboard ...

  9. git提交失败

    git push "提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支."的解决办法 本地已存在项目,需要先获取远端更新并与本地合并,再git push具体操作如下: ...

  10. HTML 概述

    一.hello world<!--根标签--><html> <!--头部--> <head> <!--标题标签--> <title&g ...