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. TCPdump抓包命令详解--摘

    http://blog.csdn.net/s_k_yliu/article/details/6665673/ http://starsliao.blog.163.com/blog/static/890 ...

  2. NSQ:分布式的实时消息平台

    NSQ是一个基于Go语言的分布式实时消息平台,它基于MIT开源协议发布,代码托管在GitHub,其当前最新版本是0.3.1版.NSQ可用于大规模系统中的实时消息服务,并且每天能够处理数亿级别的消息,其 ...

  3. shell的if else 以及大于,小于等逻辑表达式

    if ....; then .... elif ....; then .... else .... fi 大多数情况下,可以使用测试命令来对条件进行测试.比如可以比较字符串.判断文件是否存在及是否可读 ...

  4. java ObjectOutputStream

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  5. setattribute兼容

    var spanElement = document.getElementById("mySpan"); spanElement.style.cssText = "fon ...

  6. hdc cdc

    CWindowDC dc(this); HDC hdc=dc.GetSafeHdc(); using namespace Gdiplus; Graphics graphics(hdc); graphi ...

  7. mysql storage enginees

    这段时间在看<High Performance MySQL>,看到存储引擎这个地方感到很多细节比较陌生,所以总结小记一些 为 了适应各种不同的运行环境,MYSQL提供了多种不同的存储引擎( ...

  8. Excel VBA

    =COUNTIF(Y3:Y212,"=11") =SUMIF(Y3:Y212,"=11",AA3:AA212) =SUMPRODUCT((Y3:Y212=&qu ...

  9. xcode8继续愉快的使用插件

    https://github.com/inket/update_xcode_plugins https://github.com/fpg1503/MakeXcodeGr8Again xcode8增加了 ...

  10. HDFS中高可用性HA的讲解

    HDFS Using QJM HA使用的是分布式的日志管理方式 一:概述 1.背景 如果namenode出现问题,整个HDFS集群将不能使用. 是不是可以有两个namenode呢 一个为对外服务-&g ...