hdu--1258--Sum It Up(Map水过)

时间:2022-09-21 18:04:30

Sum It Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6581    Accepted Submission(s): 3451

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
 /*
     Name: hdu--1258--Sum It Up
     Copyright: ©2017 日天大帝
     Author: 日天大帝
     Date: 29/04/17 18:48
     Description: dfs,C++map水过
 */
 #include<iostream>
 #include<cstring>
 #include<map>
 using namespace std;
 void dfs(int,int,map<int,int,greater<int>>::iterator it);
 ;
 int res[MAX];
 int n,t,flag;
 map<int,int,greater<int>> mymap;
 int main(){
     ios::sync_with_stdio(false);

     while(cin>>n>>t,n||t){
         mymap.clear();
         flag = ;
         memset(res,,sizeof(res));
         ; i<t; ++i){
             int a;cin>>a;
             mymap[a]++;
         }
         cout<<"Sums of "<<n<<":"<<endl;
         dfs(,,mymap.begin());
         )cout<<"NONE"<<endl;
     }
     ;
 }
 void dfs(int sum,int ct,map<int,int,greater<int>>::iterator iter){
     if(sum == n){
         flag = ;
         cout<<res[];
         ; i<ct; ++i){
             cout<<"+"<<res[i];
         }
         cout<<endl;
     }
     for(auto it=iter; it!=mymap.end(); ++it){
         )continue;
         ; i--){
             if(sum+(i*it->first) > n)continue;
             ; k<i; ++k){
                 res[ct + k] = it->first;
             }
             auto ipoint = it;
             dfs(sum+(i*it->first),ct+i,++ipoint);
         }
     }
 }

hdu--1258--Sum It Up(Map水过)的更多相关文章

  1. HDOJ&lpar;HDU&rpar;&period;1258 Sum It Up &lpar;DFS&rpar;

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  2. HDU 1258 Sum It Up(dfs 巧妙去重)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...

  3. hdu 1258 Sum It Up (dfs&plus;路径记录)

    pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. hdu 1258 Sum It Up(dfs&plus;去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  5. HDU 1258 Sum It Up&lpar;DFS&rpar;

    题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...

  6. HDU 1258 Sum It Up

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  7. &lpar;step4&period;3&period;4&rpar;hdu 1258&lpar;Sum It Up——DFS&rpar;

    题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...

  8. HDU 4432 Sum of divisors (水题,进制转换)

    题意:给定 n,m,把 n 的所有因数转 m 进制,再把各都平方,求和. 析:按它的要求做就好,注意的是,是因数,不可能有重复的...比如4的因数只有一个2,还有就是输出10进制以上的,要用AB.. ...

  9. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  10. HDOJ&sol;HDU 1251 统计难题&lpar;字典树啥的~Map水过&rpar;

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

随机推荐

  1. Autoit3 正则表达式 匹配汉字

    关于Autoit3正则匹配汉字,在网上搜来搜去都是雷同的内容,[\u4e00-\u9fa5] 然而,Invalid all the time 直到认真钻研Help File,最终又看到了这个 http ...

  2. PHP与Golang如何通信?

    PHP与Golang如何通信? 最近遇到的一个场景:php项目中需要使用一个第三方的功能(结巴分词),而github上面恰好有一个用Golang写好的类库.那么问题就来了,要如何实现不同语言之间的通信 ...

  3. Haproxy安装配置及日志输出问题

    简介: 软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载 ...

  4. Pro Mac 如何将英文文件夹汉化为中文

    1.新建一个英文名的文件夹. 2.打开文本编辑,文本编辑 -> 预置 -> 新建文稿 -> 格式,选上纯文本,关闭预置. 3.新建了一个txt文件,在里面输入要汉化的英文名(刚才新建 ...

  5. Eclipse中如何修改SVN的地址

    在SVN服务端的IP更改后,客户端SVN的连接地址可以在Eclipse中进行修改,方法如下: 首先:在Eclipse中选择Windows-> Show View->others 就会出现[ ...

  6. CentOS安装中文输入法

    yum install "@Chinese Support" System->Preferences->Input Method,勾选"Enable inpu ...

  7. Python excel 奇怪的通信规则

    直接: for i in range(1,n+1): print(i) if xls.getCell(i,1,1)=='id': res=[] xls.xlBook.Worksheets(i).Nam ...

  8. 4&period;Linux开机设置项

    开机建议优化项: //关闭防火墙 systemctl stop firewalld systemctl disable firewalld //关闭SELinux: setenforce 0 sed ...

  9. 微信小程序:分页和加载更多

    直接上代码吧.不足之处,多多指教,一起进步 1.wxml页面的最后敲上,css自己定义 <view class="loadmore" mtype="{{mtype} ...

  10. 【不遮遮掩掩】Github上传本地代码以及常见问题解决方案

    2019.20.18更新: 把完整命令打一遍吧,不然看完太累了不是 //初始化文件夹 git init //初始化目录,把基本文件下载下来,如ignore文件 git pull --rebase or ...