PAT1048:Find Coins
1048. Find Coins (25)
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 could only use exactly two coins to pay the exact amount. Since she has as many as 105 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 two 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 (<=105, the total number of coins) and M(<=103, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1 + V2 = M and V1 <= V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output "No Solution" instead.
Sample Input 1:
8 15
1 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 14
1 8 7 2 4 11 15
Sample Output 2:
No Solution 思路 1.用桶排序的方式来存放输入的数。
2.遍历时从最小数v1开始,然后检查v2 = M - v1是否存在,没有则继续遍历直到nums[v2]存在,这时就得到满足题目要求的最小数v1,且v1 + v2 = M。 代码
#include<iostream>
#include<vector>
using namespace std; int main()
{
int N,value;
while(cin >> N >> value)
{
vector<int> nums(,);
for(int i = ;i < N;i++)
{
int number;
cin >> number;
nums[number]++;
} for(int i = ;i < ;i++)
{
if(nums[i] > )
{
nums[i]--;
if(value > i && nums[value - i] > )
{
cout << i << " " << value - i << endl;
return ;
}
nums[i]++;
} }
cout << "No Solution" << endl;
}
}
PAT1048:Find Coins的更多相关文章
- PAT1048. Find Coins(01背包问题动态规划解法)
问题描述: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...
- pat1048. Find Coins (25)
1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- [LeetCode] Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- csuoj 1119: Collecting Coins
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec Memo ...
- Coins
Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hi ...
- hdu 1398 Square Coins (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
随机推荐
- SpriteBuilder给节点添加effect在32设备上发生crash
环境为 Xcode 6.4 , cocos2D 3.0.4 , SpriteBuilder 1.4.9 在给某一节点添加Effect后,运行在真机iphone4s上发生崩溃,显示为: 可以看到整个堆栈 ...
- GCC内联函数:__builtin_types_compatible_p
#if 0 - Built-in Function: int __builtin_types_compatible_p (type1, type2) You can use the built-in ...
- 安卓笔记-- popupwindow back键不消失的问题
// 可能是一个bug ,如果不设置背景,触摸焦点外和back键都不会消失,需如下设置,并不会影响你的背景 popupWindow.setBackgroundDrawable(new ...
- Android Hal 分析
本文是基于android4.0.3.对应其他低版本的代码,可能有所差异,但基本大同小异. Android的HAL是为了保护一些硬件提供商的知识产权而提出的,是为了避开linux的GPL束缚.思路是把控 ...
- IDE
IDE(Integrated Development Environment,集成开发环境).DE集成开发环境(简称IDE)软件是用于程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用 ...
- day08_Servlet学习笔记
============================================================ 一.什么是Servlet?(非常非常重要) servlet 是运行在 Web ...
- html5中新增的非主体结构的元素
html5中出了新增了article.section.nav.aside.time主要结构元素外,还增加了一些表示逻辑结构或附加信息的非主体结构元素. 一.header元素 header元素是一种具有 ...
- List集合中元素排序
应用场景: 在开发中经常遇到要对List<Object>集合进行排序,并且是根据集合中的对象的某个属性来进行排序 --------以下就此做出的解决方案 public static ...
- MDF,了解一下
1.MDF定义 MDF,全称(Measurement Data Format),即测量数据格式,是ASAM(自动化及测量系统标准协会)定义的.MDF的网页https://www.asam.net/st ...
- Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据包访问目标所采取的路径。
Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据包访问目标所采取的路径. Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确定从一个主机到网络上其 ...