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 ...
随机推荐
- Python下载图片并保存本地的两种方式
一:使用Python中的urllib类中的urlretrieve()函数,直接从网上下载资源到本地,具体代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- 1128 聚合查询 orm字段及属性
目录 一 聚合查询 1. 级联 级联删除 级联更新 2. 聚合函数 使用 aggregate 使用场景 3. 分组查询 语法 使用 annotate 代码 4. F与Q查询 F查询 Q查询 二 ORM ...
- KaTex语法说明
参考链接: https://katex.org/docs/supported.html https://github.com/KaTeX/KaTeX/blob/master/docs/supporte ...
- P2P system: Napster
Napster structure client machines之所以叫peers是因为对于server来说这些machines是平等对待的 当你upload一首歌曲如PennyLane.mp3时, ...
- SQlAlchemy的增删改查
一.创建数据表 # ORM中的数据表是什么呢? # Object Relation Mapping # Object - Table 通过 Object 去操纵数据表 # 从而引出了我们的第一步创建数 ...
- 洛谷P1991 无线通讯网【最小生成树】
题目:https://www.luogu.org/problemnew/show/P1991 题意:有p个点的坐标,可以有s个点使得这s个点之间可以无限制通信. 要使所有点之间两两有通信的路径(可以是 ...
- js 数组中如何删除字典
区别: []表示是一个数组,如var strs = ['a','b','c'].{}表示是一个对象,比如,var obj = {name: '宙斯',sex: 1} 如何在数组中删除指定对象呢?? [ ...
- $("xxx"); 使用jQuery获得对象
$("xxx"); 使用jQuery获得对象 console.log( $ ( $ ( $ ( '.dropdown' ) ) ) ); 与下面返回的结果都是一毛一样的. con ...
- 普通页面引入React(使用和不使用JSX)
1. 不使用JSX 优点: 不用配置有关JSX的编译. 依赖语法: React.createElement(component/type, props, ...chilidren); //第一个参数可 ...
- 新的log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- OFF < FATAL < ERROR & ...