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 ...
随机推荐
- mybatis3.0-[topic10-14] -全局配置文件_plugins插件简介/ typeHandlers_类型处理器简介 /enviroments_运行环境 /多数据库支持/mappers_sql映射注册
mybatis3.0-全局配置文件_ 下面为中文官网解释 全局配置文件的标签需要按如下定义的顺序: <!ELEMENT configuration (properties?, setting ...
- Luogu P4198 楼房重建 (李超线段树)
题目 传送门 题解 首先转化成到(0,0)(0,0)(0,0)的斜率. 那么就是求多少个点是前缀最大值. 做法是线段树,用gao(i,x)gao(i,x)gao(i,x)表示在iii区间内,之前最大值 ...
- Greenplum 执行计划之广播与重分布
关联数据在不同节点上,对于普通关系型数据库来说,是无法进行连接的.关联的数据需要通过网络流入到一个节点中进行计算,这样就需要发生数据迁移.数据迁移有广播和重分布两种.在GP中,每一个广播或重分布会产生 ...
- 西门子二次开发--HMI failed to start
一.Sinumerik二次开发错误:HMI failed to start. HMI--SL Framework reported error: GUI Component could not be ...
- $noip2018$游记+考后总结
游记部分 Day-5 - Day0 敲了敲模板,打了几场模拟赛,都在颓废,其他什么都没做 Day1 早上继续写模板,水了会QQ,其他什么都没做 中午试图背模板,没成功(其实下午才发现敲的模板一个没用上 ...
- Angular2日期格式化
一:组件日期格式化: ts中调用: import {DatePipe} from "@angular/common"; @Component({ providers: [D ...
- jquery实现图片切换
<div> <img class="imgclick" src="img/pic1.png" /> </div> <s ...
- RBF、GRNN 和 PNN 神经网络在Matlab中的用法
一.RBF神经网络 RBF神经网络概述 径向基函数神经网络 与 BP 神经网络的区别在于训练过程--其参数初始化具有一定方法,并非随机,隐含层的末尾使用了径向基函数,它的输出经过加权和得到 LW2.1 ...
- maven 聚合工程 > 坑总结
maven聚合工程子项目无法构建:父工程未 install tomcat可以运行,maven 无法编译报错,检查是否添加红色语句: <dependency> <groupId> ...
- Python基础之给函数增加元信息
1. 参数注解 当写好一个函数以后,想为这个函数的参数添加一些额外的信息,这样的话,其他的使用者就可以清楚的知道这个函数应该怎么使用,这个时候可以使用函数参数注解. 函数参数注解能提示程序员应该怎样正 ...