PAT 甲级 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 1 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 (≤, the total number of coins) and M (≤, 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
题意:
假设你有N个货币,面值从1到500不等,只允许使用其中的两枚货币,然后刚好达到付款要求,如果存在多组方案,输出其中一个货币最小的方案。如果没有符合要求的方案,输出No Solution。
题解:
货币的面值只在1~500之间,那么使用一个500的int 数组存储每个面额的张数即可。
采用空间的方法,直接看另一个数是否存在。注意可能存在2个重复的数,所以要计数,而不是用bool表示。
这道题的测试点有个bug,题目明明说的是面值500,但是第三个测试点,给出了面值999的测试,所以数组不能只开500多。
AC代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int v[];//505会出现段错误
int main(){
int n,m,x;
memset(v,,sizeof(v));
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>x;
v[x]++;
}
int f=;
for(int i=;i<=;i++){
if(i!=m-i){
if(v[i]&&v[m-i]){
f=;
cout<<i<<" "<<m-i;
break;
}
}else if(i==m-i&&v[i]>=){
f=;
cout<<i<<" "<<m-i;
break;
}
}
if(!f) cout<<"No Solution";
return ;
}
PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)的更多相关文章
- PAT Advanced 1048 Find Coins (25 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏
1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1079 Total Sales of Supply Chain (25 分)(简单,不建树,bfs即可)
1079 Total Sales of Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributor ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...
随机推荐
- RedisTemplate和StringRedisTemplate的区别
今天springboot项目中用redis的时候,遇到了一个问题,用RedisTemplate这个类向redis中存储数据的时候,明明数据存进去了,也可以取出来,但是rdm就是看不到key的值,网上的 ...
- CSS float详解
前言:在我们写CSS样式的时候,float,position,display,overflow这几个关键字用得比较多. 弄清楚他们之间的原理,我们可以更高效的写出我们想要的布局. 作者:Ry-yuan ...
- MyBatis模糊查询和多条件查询
一.ISmbmsUserDao层 //根据姓名模糊查询 public List<Smbms> getUser(); //多条件查询 public List<Smbms> get ...
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part5
Use the Most Robust Selector for Cypress Tests Which selectors your choose for your tests matter, a ...
- 加入redis缓存的 优 缺 点以及解决方案
redis(支持十万级的并发) 优点: 1.减轻数据库压力 2.增强系统的并发量 3.提高用户体验 缺点: 1.数据不一致(延迟更新) ...
- HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9
/* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...
- guava字符串工具 Strings 校验补全 转换null和""
public class StringsTest { public static void main(String args[]){ //1.补右全(Strings.padEnd方法) String ...
- 原生JS实现简易随机点名功能
定时器的工作原理,这里将用引用How JavaScript Timers Work中的例子来解释定时器的工作原理,该图为一个简单版的原理图.· 上图中,左侧数字代表时间,单位毫秒:左侧文字代表某一个操 ...
- greenplum(postgresql) 数据字典
greenplum是基于postgresql开发的分布式数据库,里面大部分的数据字典是一样的.我们在维护gp的时候对gp的数据字典比较熟悉,特此分享给大家.在这里不会详细介绍每个字典的内容,只会介绍常 ...
- 三十一.MySQL存储引擎 、 数据导入导出 管理表记录 匹配条件
1.MySQL存储引擎的配置 查看服务支持的存储引擎 查看默认存储类型 更改表的存储引擎 设置数据库服务默认使用的存储引擎 1.1 查看存储引擎信息 mysql> SHOW ENGINES\G ...