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
 #include<cstdio>
using namespace std;
const int N = ;
int hashTB[N] = {, };
int main(){
int n, m, v;
scanf("%d %d", &n, &m);
for(int i = ; i < n; i++){
scanf("%d", &v);
hashTB[v]++;
}
for(int i = ; i < N; i++){
if(hashTB[i] && hashTB[m - i]){
if(i == m - i && hashTB[i] <= ){
continue;
}
printf("%d %d\n", i, m - i);
return ;
}
}
printf("No Solution\n");
return ;
}

总结:

1、本题题意:给出拥有的硬币,给出要凑出的面额M,找出两个硬币且他们的和为M。显然暴力法很可能超时,所以关键是要想到用哈希表。

2、由于每个钱的面额都小于500,但M可能大于500,故hashTB的大小最好为1000,否则用指针i遍历时,M - i可能越界。

3、此题还可以用二分法。先对所有面值进行排序,遍历a[0]、a[1]……,对于每一个a[i],寻找是否存在M - a[ i ]且 M - a[i] 不是a[i],若找到则输出。

A1048. Find Coins的更多相关文章

  1. PAT甲级——A1048 Find Coins

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  2. A1048 Find Coins (25 分)

    一.技术总结 首先初看题目有点没读懂,题目大致意思是小明有很多个硬币不同面值的,但是现在他要到商家这里换新的面值, 且商家有一个规定,一个新的硬币必须要你两个硬币面值相加等于的来换,这一有第一个问题产 ...

  3. PAT_A1048#Find Coins

    Source: PAT A1048 Find Coins (25 分) Description: Eva loves to collect coins from all over the univer ...

  4. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  5. 1048 Find Coins (25 分)

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

  6. [LeetCode] Arranging Coins 排列硬币

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  7. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  8. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  9. csuoj 1119: Collecting Coins

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec  Memo ...

随机推荐

  1. RabbitMQ 延时消息设计

    问题背景 所谓"延时消息"是指当消息被发送以后,并不想让消费者立即拿到消息,而是等待指定时间后,消费者才拿到这个消息进行消费. 场景一:客户A在十二点下了一个订单,我想半个小时后来 ...

  2. 针对负载均衡集群中的session解决方案的总结

    在日常运维工作中,当给Web站点使用负载均衡之后,必须面临的一个重要问题就是Session的处理办法,无论是PHP.Python.Ruby还是Java语言环境,只要使用服务器保存Session,在做负 ...

  3. Centos下安装破解confluence6.3的操作记录

    confluence是一个专业的企业知识管理与协同软件,可以用于构建企业wiki.通过它可以实现团队成员之间的协作和知识共享.现在大多数公司都会部署一套confluence,用作内部wiki.现在co ...

  4. 安装Visual Studio开发平台

    1.找一个VS2013的安装包,下载到D盘上,勾选相应的选项安装. 安装的过程很漫长,至少需要一个小时. 2.安装已完成,启动. . 3.登录. \ 4启动VS2013. 5.新建c#类库 6.输入代 ...

  5. 分页查询信息(使用jdbc连接mysql数据库实现分页查询任务)

             分页查询信息       使用jdbc连接mysql数据库实现分页查询任务 通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上. 本项目 ...

  6. 对wordcount单词字母部分的修改

    原始代码: int s; s = ch; switch (s) { case 'a':letter[0]++; break; case 'b':letter[1]++; break; case 'c' ...

  7. 第九次Scrum meeting

    第九次Scrum  meeting 任务及完成度: 成员 12.31 1.1 陈谋 任务1040:完成stackoverflow的数据处理后的json处理(99%) 任务1114-1:完成对网页数据的 ...

  8. 第三个Sprint冲刺第3天

    成员:罗凯旋.罗林杰.吴伟锋.黎文衷 组内各成员加紧完成自己的工作.

  9. pandas修改全列的时间格式 无需使用apply

    df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式

  10. Redis的五种数据类型

    官方的几篇很好的文章: https://redis.io/topics/data-types https://redis.io/topics/data-types-intro https://redi ...