Sum It Up(HDU-1258)

sum it up ! 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. 立即下载
Problem Description
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.
 

Input
The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1,...,xn. If n=0 it signals the end of the input; otherwise, 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.
 

Output
For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order. 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.
 

Sample Input
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
 

Sample Output
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
 
题目大意:输入一个sum,和n个数据,输出所有数据组合相加和为sum的组合,输出不重复;
思路:普通的dfs,用set判重;
代码如下:
#include<bits/stdc++.h>
using namespace std;
int sum,a[15],n,b[15],flag;
set<vector<int> >p;
void dfs(int s,int pos,int k)
{
    if(s==sum)
    {
        flag=0;
        vector<int>v;
        for(int i=0;i<k;i++)
            v.push_back(b[i]);
        if(p.find(v)==p.end())
        {
            for(int i=0;i<k-1;i++)
            cout<<b[i]<<'+';
            cout<<b[k-1]<<endl;
            p.insert(v);
        }
        return;
    }
    for(int i=pos+1;i<n;i++)
    {
        b[k]=a[i];
        dfs(s+a[i],i,k+1);
    }

}
int main()
{
    while(cin>>sum>>n&&sum&&n)
    {
        flag=1;
        for(int i=0;i<n;i++)
            cin>>a[i];
        cout<<"Sums of "<<sum<<':'<<endl;
        dfs(0,-1,0);
        if(flag)
            cout<<"NONE\n";
    }
    return 0;
}



weblogic 集群搭建,双机部署,相关问题记录 4.5.集群搭建1 参考网址https://wenku.baidu.com/view/76365129cd1755270722192e453610661ed95aa4.html IP地址 端口 角色 192.168.1.160 9001 AdminServer ... 阅读详情

相关推荐

恒玄BES2700ZP开发实战:从零搭建嵌入式音频系统

本文详细介绍了恒玄BES2700ZP蓝牙音频SoC的开发实战,从环境搭建、SDK架构解析到蓝牙音频、AI语音集成及低功耗优化。重点探讨了如何利用其多核架构实现高性能音频处理和LE Audio支持,为开发者提供从零构建嵌入式音频系统的完整指南。

o1p2q3r的博客 925

hdoj1258

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1838    Accepted Submission(s): 920 Problem Description Given a specified

◆◇丶生如夏花。谁来订阅我的悲伤 648

基于BFQ19S的共射振荡器设计(克拉泼电容三点式)

根据理论,设计出如下LC振荡器: 一、设计初期是使用= 400Mhz作为目标值计算的, 但由于计算值与实际值有差别,所以使用计算值附近的元器件代替使用;最终该电路的L1=82nH , D1 = 5.6pF, C11=18pF, C12 = 18pF ; 另外R3= 600Ω, R4取27k , C9取10pF; 该电路实际的震荡频率为主要为L1,D1 决定,所以其震荡频率约为 == 235Mhz; 二、由于实际得到的频率比目标频率低太多,所以调整电路使其能达到400M左...

rzchong1988的专栏 3709

Hdu 1258 Sum It Up :DFS 去重

Sum It Up 题目描述: 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

Fluence_YHL 333

Python爬取腾讯视频m3u8加密流实战(附完整代码)

本文提供了一份详细的Python实战教程,指导如何通过抓包分析、模拟请求头与载荷,破解腾讯视频的m3u8加密流防盗链机制,并附上完整代码实现视频片段的下载与合并。教程涵盖从侦察到代码实现的完整流程,并包含参数动态化、多线程下载等高级技巧与避坑指南,旨在帮助开发者掌握流媒体下载的核心技术。

weixin_29090089的博客 1646

液晶电视维修必看:TCON逻辑板故障排查全指南(附常见问题解决方案)

本文详细解析了液晶电视TCON逻辑板的工作原理与故障排查方法,涵盖LVDS信号处理、DC-DC电压系统等核心知识。提供从基础测量到高级维修的完整指南,帮助技术人员快速诊断黑屏、竖线等常见故障,并给出预防性维护建议,提升维修效率与成功率。

weixin_42535461的博客 619

1.29题目翻译

链接:https://acs.jxnu.edu.cn/problem/HDU1250 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

weixin_61393233的博客 252

大数据面试题(附答案,持续更新...)

说说对hadoop的理解,都有哪些组件,分别是干什么的 还了解大数据其他组件嘛? kafka在什么地方需要用到zookeeper 了解HBase吗? 说下spark中的transform和action 为什么spark要把操作分为transform和action spark中有了RDD,为什么还要有Dataframe和DataSet? 了解函数式编程吗?说下c/c++和scala这种函数式编程语...

xiaozhaoshigedasb的博客 9948

POJ 1564 Sum It Up——DFS

POJ DFS基础题

Vontroy 1065

仪表数字图像识别的传统方法

虽然现有很多机器学习算法能够准确地识别数字,但是传统方法对于一些需要离线识别且计算能力有限的下位机设备来说,仍然有非常不错的效果。因此,今天就向大家讲解一下传统的数字识别方法在仪表读数方面的应用。 1.图像预处理技术 1.1 主要流程 图像处理依次需要进行图片预处理、字轮区域定位、字符分割和字符识别等过程。其中图像处理流程如下图所示。 2. 详细过程 2.1 灰度化 为了减小图像原始数据量,本系...

XYZ 4524

华为OD机试真题 新系统【网络数据包收发处理】

华为OD机试 新系统真题【网络数据包收发处理】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【网络数据包收发处理】多语言题解。

qq_45776114的博客 447

POJ 1564 Sum It Up (DFS+剪枝)

Sum It Up Time Limit: 1000MS   Memory Limit: 10000K

code_or_code 1474

全志A133的AndroidSDK

本小节来学习如何下载全志安卓SDK,SDK的目录,编译和烧录提示:以下是本篇文章正文内容,下面案例可供参考以上就是整个SDK的目录和编译烧录,目的在于让初学者快速了解整个源码的目录和编译出镜像喜欢就订阅吧。

qq_24093081的博客 4058

HDU-1258-Sum It Up

HDU-1258-Sum It Up http://acm.hdu.edu.cn/showproblem.php?pid=1258 注意每个数只能用一次,且不能出现重复的等式 #include #include #include int a[20],ans[20]; int sum,n,flag; void dfs(int x,int count,int m) { int i,last;

菜鸟的程序人生 2195

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

hdu 1258 Sum It Up

Sum It Up 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1258 题目的意思:给一个sum的值,给一个n,接下来有n个数 这几个数随机组合等于sum时,输出怎么样组合的。 注意:不能有重复的 解题思路:用DFS深搜,这个我写的时候都把自己搞晕了,看了别人的思路才憋出来,屡思路就屡了半天,还是去重的地方理解不深,还是太水啊 Sampl

xuesen1995的专栏 194

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

DFS HDU 1258 Sum It Up

1258 Sum It Up 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1258 题目意思: 给定一个数t,和n个数字,从n个数字选取 求和使得结果为t For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums th...

Wei Masking的博客 222

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Sum It Up hdu 1258

Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7345 Accepted Submission(s): 3874 Problem Description Given a specified tota...

ljyanjy 314

HDU 1258 Sum It Up (还是DFS)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5691    Accepted Submission(s): 2967 Problem Description Given a specifie

AC_Dreameng 1949

HDU 1258 Sum It Up

题目链接:传送门 Problem Description 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...

贾作真时真亦贾的博客 183

【HDU 1258】Sum It Up(DFS,去重技巧)

思路: 这道题和HDU-1342 Lotto很相似,都在DFS中套用了两层递归。1342那题规定了输出元素个数,只需求出所有排列。因此搜索出口很方便找到。而此题是要求寻找符合“和等于所给值”的所有排列,甚至可能找不到。 上代码,大致讲解一下: void DFS(int num,int pos,int sum) { if(sum==N) { judge=true;

Chen_yuazzy的博客 2196

HDU-1258 Sum It Up

题意:给出sum和n个数,找出满足m(m<=n)个数相加等于sum。 思路:dfs. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1258 View Code 1 #include <cstdio> 2 #include <cstring> 3 #include <cstd...

bancaidun1539的博客 133

hdu-1258 Sum It Up

http://acm.hdu.edu.cn/showproblem.php?pid=1258 题意:给定一个目标数t,和n个数,并且按非升序排列,问n个数里面有多少种情况之和等于t,并且按非升序输出。 思路:dfs三个参数,k,sum,num,k表示当前到这个数,sum表示当前数的和,num表示当前数的个数。麻烦一点的是判重,比如第一个样例3 1,只能输出一次。具体见代码。 #include

有一种力量叫乐观 636

hdu 1258 Sum It Up(DFS)

题目链接:hdu1258 水题一道..................... #include #include #include using namespace std; int map[15],v[15],ans[15]; int sum,n,flag; void dfs(int x,int num,int cur) { int i; if(num == sum)

no pains,no gains 1094

HDU1258:Sum It Up(DFS)

Problem Description 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], t

ACM!荣耀之路! 6658

hdu 1258 Sum It Up(回溯算法)

<br />题意:从给定的n个正整数中选出m个数(m <= n)使得这m个数的和为给定的total<br />解法:回溯<br />/* Author: ACb0y Date: 2010-11-11 ProblemId: hdu 1258 Sum It Up Result: 3181578 2010-11-11 20:42:18 Accepted 1258 0MS 368K 1461 B G++ ACb0y */ #include <iostream> #include <algori

个人独立博客http://www.acb0y.cn/ 1508
上一篇: Lake Counting(POJ-2386)
下一篇: 漫步校园(HDU-1248)
绿蚁.r
博客等级 码龄10年 4粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值