PAT 解题报告 1048. Find Coins (25)
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:hash
首先,硬币中币值不小于商品价格的可以过滤掉。遍历过程中,使用 hash 标记的方法,设定一个coins[MAXVALUE]
(其中 MAXVALUE 为商品价格的最大值)记录已有的币值种类, 在遍历的过程中,一边将读入的硬币增加到coins[]
中,一边计算满足条件的最小币值。
这个方法速度很快,数据读完,就差不多得到结果了。
思路2:先排序,然后两头扫
经典的2sum求和问题, 先给数组排序, 然后头尾指针分别从前后开始扫, 扫到的两个数的和若比给定的值要小, 那么头指针向后移动一步, 如果比给定的值大, 那么尾指针向前移动一步. 如果正好找到了, 那么就是他了, 返回.
hash
#include <cstdio> using namespace std; const int MAXVALUE = ;
int a[MAXVALUE]; int main()
{
int min = MAXVALUE;
int n, m;
int tmp;
scanf("%d%d", &n, &m);
for (int i = ; i != n; ++i) {
scanf("%d",&tmp);
if (tmp < m) {
int tmpMin = tmp < m - tmp ? tmp : m - tmp;
if (a[m - tmp] == && tmpMin < min) {
min = tmpMin;
}
a[tmp] = ;
}
} if (min != MAXVALUE) printf("%d %d\n", min, m - min);
else printf("No Solution"); return ;
}
两头扫
#include <cstdio>
#include <algorithm>
#include <vector> #define REP(i,n) for(int i=0;i<(n);++i)
using namespace std; vector<int> vals; int main(void) {
int N, M;
scanf("%d %d", &N, &M);
vals.resize(N);
REP(i, N) scanf("%d", &vals[i]);
int i=;
int j=N-;
sort(vals.begin(), vals.end());
bool found = false;
while(i < j) {
int sum = vals[i] + vals[j];
if(sum == M) {
found = true;
break;
}
else if(sum < M) {
i++;
}
else
j--;
}
if(found) {
printf("%d %d\n", vals[i], vals[j]);
}
else {
printf("No Solution\n");
}
return ;
}
PAT 解题报告 1048. Find Coins (25)的更多相关文章
- PAT 解题报告 1051. Pop Sequence (25)
1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...
- PAT (Advanced Level) 1048. Find Coins (25)
先对序列排序,然后枚举较小值,二分较大值. #include<iostream> #include<cstring> #include<cmath> #includ ...
- 【PAT甲级】1048 Find Coins (25 分)(二分)
题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出&qu ...
- 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 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- PAT Advanced 1048 Find Coins (25) [Hash散列]
题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- PAT 解题报告 1047. Student List for Course (25)
1047. Student List for Course (25) Zhejiang University has 40000 students and provides 2500 courses. ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
随机推荐
- 免费手机号码归属地API查询接口和PHP使用实例分享
免费手机号码归属地API查询接口和PHP使用实例分享 最近在做全国性的行业分类信息网站,需要用到手机号归属地显示功能,于是就穿梭于各大权威站点之间偷来了API的接口地址. 分享出来,大家可以用到就拿去 ...
- RT-Thread信号量实际运用—按键点灯
上面是魔笛开发板上 LED 和按键的 IO 分布,我们通过信号量的方法来同步按键线程和LED 线程,实现当 enter 键按下后,点亮或关闭 LED 的动作. /******************* ...
- python 动态加载module、class、function
python作为一种动态解释型语言,在实现各种框架方面具有很大的灵活性. 最近在研究python web框架,发现各种框架中需要显示的定义各种路由和Handler的映射,如果想要实现并维护复杂的web ...
- Overengineering
https://en.wikipedia.org/wiki/Overengineering Overengineering (or over-engineering) is the designing ...
- CLH锁 、MCS锁
一.引文 1.1 SMP(Symmetric Multi-Processor) 对称多处理器结构,指服务器中多个CPU对称工作,每个CPU访问内存地址所需时间相同.其主要特征是共享,包含对CPU,内存 ...
- js合计
Js合计行: 可以先循环行,然后按行获取这行带有你定义的class的td,取得这些td的 text后相加,最终赋值到这行的“合计”单元格就行了 var trslength = $("#dat ...
- C# 判断字符串是否为日期格式
判断字符串内容是否为日期格式,并返回一个日期变量 string str; DateTime dtTime; if (DateTime.TryParse(str, out dtTime)) { //st ...
- 算法训练 Hankson的趣味题
算法训练 Hankson的趣味题 时间限制:1.0s 内存限制:64.0MB 问题描述 Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Han ...
- Invitation Cards---poj1511(spfa)
题目链接:http://poj.org/problem?id=1511 有向图有n个点m条边,求点1到其他n-1个点的最短距离和+其他点到点1的最小距离和: 和poj3268一样,但是本题的数据范围较 ...
- oracle入门-%的用法
vempno emp.empno%type; 例如上面的这句话,你的vempno就是你定义的变量,和面的那个emp是你数据库里面存在的表,他的表里面有意个empno字段,然后%type就是empno的 ...