1068. Find More Coins
来源:互联网 发布:c语言界面的汉化 编辑:程序博客网 时间:2024/06/05 05:44
1068. Find More Coins (30)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 104 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=104, the total number of coins) and M(<=102, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the face values V1 <= V2 <= ... <= Vk such that V1 + V2 + ... + Vk = M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.
Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k >= 1 such that A[i]=B[i] for all i < k, and A[k] < B[k].
Sample Input 1:8 95 9 8 7 2 3 4 1Sample Output 1:
1 3 5Sample Input 2:
4 87 2 4 3Sample Output 2:
No Solution
用map记录各种硬币的数量,不用也行,M不大于100,用数组也不大。用DFS从小到大找,找到的第一个答案即可以结束查找。
#include<iostream>#include<algorithm>#include<map>#include<vector>using namespace std;map<int,int>amount;vector<int>ans;vector<int>tmp;int n,m;void dfs(int cur,int sum){if(sum<0)return;else if(sum==0){ans=tmp;return;}for(int i=cur;i<=sum;++i){if(!ans.empty())return ;if(amount[i]>0){tmp.push_back(i);amount[i]--;dfs(i,sum-i);tmp.pop_back();amount[i]++;}}}int main(){cin>>n>>m;for(int i=0;i<n;++i){int num;cin>>num;amount[num]++;}dfs(1,m);if(ans.empty())cout<<"No Solution"<<endl;else{cout<<ans[0];for(int i=1;i<ans.size();++i)cout<<" "<<ans[i];cout<<endl;}return 0;}
- 1068. Find More Coins
- 1068. Find More Coins
- 1068.Find More Coins
- 1068. Find More Coins
- 1068. Find More Coins
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- PAT 1068. Find More Coins
- 1068. Find More Coins (30)
- PAT 1068. Find More Coins
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 1068. Find More Coins (30)
- 请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
- 获取毫秒时间戳
- 实用!开发者的 Vim 插件(一)
- STL源码分析(2) -- list.h分析(1)
- 领悟自定义风采,ScrollView源码完全解析
- 1068. Find More Coins
- tensorflow学习笔记(十二):Normalization
- 选择排序
- 复杂声明的理解1
- 自动文档摘要评价方法:Edmundson,ROUGE
- 静态库与动态库的创建与使用
- 关于直播技术的转载
- android demo(三):simpleAdapter
- redis + spring 配置