博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 解题报告 1048. Find Coins (25)
阅读量:7076 次
发布时间:2019-06-28

本文共 2963 字,大约阅读时间需要 9 分钟。

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 151 2 8 7 2 4 11 15

Sample Output 1:

4 11

Sample Input 2:

7 141 8 7 2 4 11 15

Sample Output 2:

No Solution
题意

给定一系列硬币,以及一个商品的价格。要求从硬币中找到两个硬币让他们的组合等于商品的价格。如果有多个,输出有用最小单个值的硬币组合。

分析

思路1:hash

首先,硬币中币值不小于商品价格的可以过滤掉。遍历过程中,使用 hash 标记的方法,设定一个coins[MAXVALUE](其中 MAXVALUE 为商品价格的最大值)记录已有的币值种类, 在遍历的过程中,一边将读入的硬币增加到coins[]中,一边计算满足条件的最小币值。

这个方法速度很快,数据读完,就差不多得到结果了。

思路2:先排序,然后两头扫

经典的2sum求和问题, 先给数组排序, 然后头尾指针分别从前后开始扫, 扫到的两个数的和若比给定的值要小, 那么头指针向后移动一步, 如果比给定的值大, 那么尾指针向前移动一步. 如果正好找到了, 那么就是他了, 返回.

 

hash

#include 
using namespace std;const int MAXVALUE = 1001;int a[MAXVALUE];int main(){ int min = MAXVALUE; int n, m; int tmp; scanf("%d%d", &n, &m); for (int i = 0; i != n; ++i) { scanf("%d",&tmp); if (tmp < m) { int tmpMin = tmp < m - tmp ? tmp : m - tmp; if (a[m - tmp] == 1 && tmpMin < min) { min = tmpMin; } a[tmp] = 1; } } if (min != MAXVALUE) printf("%d %d\n", min, m - min); else printf("No Solution"); return 0;}

两头扫

#include 
#include
#include
#define REP(i,n) for(int i=0;i<(n);++i)using namespace std;vector
vals;int main(void) { int N, M; scanf("%d %d", &N, &M); vals.resize(N); REP(i, N) scanf("%d", &vals[i]); int i=0; int j=N-1; 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 0;}

 

转载于:https://www.cnblogs.com/549294286/p/3574220.html

你可能感兴趣的文章
定义一个二维字符串数组,存储5个身份证号码,并输出
查看>>
堆排序
查看>>
iPhone/iPad被停用怎么办 3招轻松解锁
查看>>
20060726: 最近的事情
查看>>
webMethods中的document格式——Dictionary
查看>>
CCF201412-2 Z字形扫描 java(100分)
查看>>
js变量作用域和闭包的示例
查看>>
适配器模式(Adapter)
查看>>
iOS设计指南
查看>>
修复 DSL 自动开关机的问题 ( NDSL / iDSL 自动开机 自动关机 修理)
查看>>
linux下SVN忽略文件/文件夹的方法
查看>>
Nginx和Apache
查看>>
socket接受大的数据
查看>>
python学习日记2
查看>>
学习日记0821组合 多态 封装
查看>>
怎么获取红米6 Pro的root权限
查看>>
poj 3376 Finding Palindromes
查看>>
proxy vue3.0
查看>>
Web大前端环境搭建
查看>>
js和JQuery区别
查看>>