hdoj1258

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. 立即下载

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

Source
 

Recommend
JGShining
#include<iostream>
#include<cstring>
using namespace std;
int number[13];
int ans[13];
int t;
int n;
int len;
int flag;

void dfs(int start, int sum) {
    if (sum == t) {
        flag = 1;
        for (int i = 0; i < len - 1; i++)
            cout << ans[i] << '+';
        cout << ans[len - 1] << endl;
        return;
    }
    for (int i = start; i < n; i++) {
        int tmp = sum + number[i];
        if (tmp > t)
            continue;
        if (i > start && number[i] == number[i - 1])
            continue;
        ans[len++] = number[i];
        dfs(i + 1, tmp);
        len--;
    }
}

int main(void) {
    while (cin >> t >> n, n) {
        len = 0;
        flag = 0;
        for (int i = 0; i < n; i++)
            cin >> number[i];
        cout << "Sums of " << t << ':' << endl;
        dfs(0, 0);
        if (!flag)
            cout << "NONE" << endl;
    }
    return 0;
}


Ollama内网离线部署大模型实战:从模型文件到私有化服务 本文详细介绍了如何在内网离线环境下,利用Ollama部署私有化大模型服务。内容涵盖从获取GGUF格式模型文件、手动安装Ollama、编写核心Modelfile配置文件,到创建运行模型、性能调优及安全加固的全流程实战指南,旨在帮助企业安全、可控地构建内部AI能力。 阅读详情

相关推荐

高效低功耗设计:PMOS电子开关在墨水屏与ADC采样电路中的创新应用

本文深入探讨了PMOS电子开关在低功耗设计中的创新应用,特别是在墨水屏和ADC采样电路中的关键作用。通过详细分析PMOS的导通机制和近乎零静态功耗特性,展示了其在待机低功耗场景下的显著优势,并提供了实用的电路设计技巧和元器件选型建议,帮助工程师实现极致的功耗优化。

weixin_29193359的博客 169

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 334

从URDF到仿真动画:基于Simulink、Simscape与S-Function的机械臂一体化建模与控制

本文详细介绍了从URDF文件到高保真仿真动画的一体化机械臂建模与控制流程。通过整合Simulink、Simscape与S-Function,构建了包含物理特性的虚拟机械臂模型,并实现了自适应滑模等高级控制算法的嵌入与验证。该流程打通了从机械设计到控制算法验证的闭环,为机器人开发提供了高效的仿真平台。

cnn8visionary的博客 706

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

保姆级教程:不用看古早代码,5分钟搞懂泛微ecology9附件存储与接口调用

本文详细解析了泛微ecology9附件存储与接口调用的核心原理,提供从基础概念到实战操作的完整指南。通过拆解文档化附件设计理念、解析RESTful接口参数,并给出Python代码示例,帮助开发者快速掌握附件上传与业务集成的关键技巧,提升二次开发效率。

weixin_28688385的博客 364

SAC安装与相关库文件(.so)的安装

摘要 SAC: Seismic Analysis Code 是用于地震信号处理的一套软件。 本文将介绍SAC的安装,以及安装时遇到的一些问题。主要是一些动态库(lib**.so)找不到的问题。 本文的环境 64位的 Ubuntu 16.04 64位的系统是安装出现问题的主要原因 1. SAC获取与安装 SAC 的安装,只需要解压,然后修改环境变量即可!!!不需要任何编译!!! 1.1. SAC 获取与解压 SAC软件获取地址:http://ds.iris.edu/ds/nodes/dmc/forms/s.

WangCY的博客 4340

第九章:致我们终将逝去的青春(深度优先搜索问题)

描述 /青 春 是 用 来 追 忆 的 /当 你 怀 揣 着 她 时 /她 一 文 不 值 /只 有 将 她 耗 尽 后 /再 回 过 头 看 /一 切 才 有 了 意 义 /爱 过 我 们 的 人 和 伤 害 过 我 们 的 人 /都 是 我 们 青 春 存 在 的 意 义 在ACM中找寻青春的意义: Given a specified total t and a list of n integers, find all distinct sums using numbers from the list

我的博客 334

ESP32搭配LVGL显示图片的两种方法详解:内部存储 vs 外部文件

本文详细介绍了ESP32搭配LVGL显示图片的两种方法:内部存储与外部文件。内部存储方案适合快速响应的小图片,而外部文件方案则适用于需要灵活更新和大容量图片的场景。通过对比分析,帮助开发者根据项目需求选择最优方案,提升嵌入式系统的UI性能。

weixin_30399821的博客 525

POJ 1564 Sum It Up——DFS

POJ DFS基础题

Vontroy 1065

24、水下传感器网络中混合LEACH协议与遗传算法获取路由节点

本文提出了一种结合遗传算法与LEACH协议的混合路由方案,用于提升水下无线传感器网络(UWSN)的能量利用效率和数据传输稳定性。通过遗传算法优化簇头选择与路径规划,有效避免节点过早死亡,减少能量消耗,在200个节点场景下显著提升网络寿命。相比传统LEACH协议,该方案具有更高的能量利用率、更强的环境适应性,并支持传输历史记录与并行传输序列。实验结果显示,该方案在总能量为2000焦耳时剩余能量达75.6%,远优于LEACH的34.8%能耗表现。适用于海洋监测、石油平台监控及水下灾害预警等复杂环境应用。

i8j9k0l1的博客 116

手把手教你学Simulink实例--基于Simulink的电力电子系统电池管理系统(BMS)设计与仿真实例

本文详细介绍了基于Simulink的电池管理系统(BMS)设计与仿真全过程。首先阐述了BMS在安全防护、寿命延长和性能优化方面的必要性,分析了新能源汽车、光伏储能等典型应用场景的技术指标要求。接着详细讲解了Thevenin等效电路和多电平电池模型构建方法,并给出SOC估算、故障诊断等核心算法的MATLAB实现代码。通过设计正常充放电、过充保护等多场景仿真测试,验证了系统在SOC跟踪精度(误差<1.5%)和故障响应速度(<10ms)方面的优异性能。文章还提出了参数优化方法和未来AI融合、数字孪生等发展方向,提

xiaoheshang_123的博客 385

POJ 1564 Sum It Up (DFS+剪枝)

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

code_or_code 1474

符合DO-178C和DO-331的机载软件研制体系

DO-178C的核心要求软件开发生命周期:DO-178C定义了软件开发的生命周期过程,包括需求获取、设计、实现、验证和确认等。它为机载软件开发提供了一套系统化的指导,确保在开发过程中考虑安全性和可靠性。安全等级(DAL):根据系统的重要性,DO-178C将软件划分为不同的开发保障等级(DAL A到E),DAL A的要求最为严格,适用于关键安全功能,而DAL E的要求较低,适用于非关键功能。DO-178C在非基于模型软件开发中的应用。

qq_41854911的博客 983

HDOJ(HDU).1258 Sum It Up (DFS)

每组数据给出要凑出的目标数字num和数字个数n,然后依次给出n个数字。要求从n个数字中选出若干个数字,是的数字之和为nun。**重复的组合只输出一次。** 和之前做过的选数字的题目类似,也可以采用DFS的思想来做。这道题与[ HDOJ.1342 Lotto [从零开始DFS(0)]](http://blog.csdn.net/pengwill97/article/details/54850852)及其的相似:每个数

Pengwill's Blog 955

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

HDOJ 1258

1 #include 2 #includestring> 3 #include 4 #include 5 #include 6 #include 7 #define MAXN 20 8 using namespace std; 9 int a[MAXN], temp[MAXN], vis[MAXN]; 10 int T, N, flag, sum; 11

PsTree 444

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Hdoj 1258 Sum it up

题目请点击这里 题意:给定一个数sum,和n个数,问sum可以由这里面哪些数来表示?(等式不能重复,典型dfs) ac代码如下: (开始把sums写成sum,wa了4发,太粗心了我)#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<iostream> usi

I good vegetable a! 795

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

hdoj 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): 5260    Accepted Submission(s): 2745 Problem Description Given a specified to

平平淡淡才是真 409

hdoj - 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,2,1,1], t...

weixin_30707875的博客 96

hdoj 1258 SUM IT UP

程序的思想是:输入数据是,先使用快排对其从大到小进行排序,然后记录相同数据的个数,比如4 3 3 2 2 1 1,最后的数据变成4 3 2 1 ,并且同时数据的个数f[]变成1 2 2 2然后就是遍历,相同的数据如果不能得到最后的结果,下一次就不会遍历。//剪枝有这几个首先:从大到小排序,剪枝1再者:如果当前的sum比要遍历的数据小,则跳过这个数据利用一个vector来记录结果#include

Enigma_hao的专栏 1106

HDOJ1258 又是一道深搜入门题

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

勇敢前进的ACMer 393

【HDOJ】1258 Sum It Up

典型的深搜,剪枝的时候需要跳过曾经搜索过的相同的数目,既满足nums[i]=nums[i-1]&&visit[i-1]==0,visit[i-1]==0可以说明该点已经测试过。 1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 1005 5 6 int ...

weixin_34198797的博客 112

hdoj 1258(DFS)

#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; int a[100],n,m,b[100],flag; void printf(int k) ...

weixin_30817749的博客 91

hdoj-1258 Sum It Up

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

落辰哀的博客 233

HDOJ 1258 确定比赛名次 (拓扑排序)

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17394    Accepted Submission(s): 6948 Problem Description 有N个比赛队(1   I

jianjun的专栏 403

HDOJ1258. Sum It Up(DFS+剪枝)

Sum It UpTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7203    Accepted Submission(s): 3796Problem DescriptionGiven a specified total t and a ...

cr496352127的博客 264

hdoj--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): 5534    Accepted Submission(s): 2890 Problem Description Given a specified

playboy307 338
上一篇: hdoj1195
下一篇: poj1018
lulining
博客等级 码龄15年 8粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值