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)的更多相关文章

  1. 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 ...

  2. PAT (Advanced Level) 1048. Find Coins (25)

    先对序列排序,然后枚举较小值,二分较大值. #include<iostream> #include<cstring> #include<cmath> #includ ...

  3. 【PAT甲级】1048 Find Coins (25 分)(二分)

    题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出&qu ...

  4. 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 ...

  5. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  6. PAT Advanced 1048 Find Coins (25) [Hash散列]

    题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...

  7. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  8. PAT 解题报告 1047. Student List for Course (25)

    1047. Student List for Course (25) Zhejiang University has 40000 students and provides 2500 courses. ...

  9. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

随机推荐

  1. NSQ部署

    一.      简介 NSQ主要有三个主要程序和一个Web服务程序: nsqd:是守护进程,接收,缓存,并投递消息给客户端 nsqlookupd:是一个守护进程,为消费者提供运行时发现服务,来查找指定 ...

  2. 如何获取DIV的id

    $(obj).attr("id");参数可以是id也可以是其他例如name等属性

  3. 【转】javascript中this的四种用法

    在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this 出现的场景 ...

  4. JAVA内省(Introspector)

    什么是Java内省:内省是Java语言对Bean类属性.事件的一种缺省处理方法. Java内省的作用:一般在开发框架时,当需要操作一个JavaBean时,如果一直用反射来操作,显得很麻烦:所以sun公 ...

  5. php判断爬虫

    function checkrobot($useragent = ''){ static $kw_spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos ...

  6. isset和empty比较

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  7. 创建第一个ArcGIS API for Silverlight应用

    原文:创建第一个ArcGIS API for Silverlight应用 在完成前面的开发环境搭建以后,接下来实现我们的第一个ArcGIS API forSilverlight应用程序. 接下来我们一 ...

  8. [LeetCode]题解(python):055-Jump Game

    题目来源 https://leetcode.com/problems/jump-game/ Given an array of non-negative integers, you are initi ...

  9. Surround the Trees---hdu1392(凸包GraHam模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:有n棵树,每棵树有一个坐标,想用一些绳子把这些树包含起来,求需要绳子的长度: 就是求凸包的 ...

  10. 开发报表时将已有User做成下拉列表,第一项为label为ALL,value为null

    SELECT 'All' AS LABLE_NAME, NULL AS USER_NAMEUNION ALLSELECT USER_NAME AS LABLE_NAME, USER_NAME from ...