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. js值----你所不知道的JavaScript系列(6)

    1.数组 在 JavaScript 中,数组可以容纳任何类型的值,可以是字符串.数字.对象(object),甚至是其他数组(多维数组就是通过这种方式来实现的) .----<你所不知道的JavaS ...

  2. SKINNY加密算法详解(无代码,仅加密)

    原作者论文请参考<The SKINNY Family of Block Ciphers and Its Low-Latency Variant MANTIS> 地址为:https://li ...

  3. zabbix监控交换机、防火墙等网络设备

    zabbix3.4.4监控交换机/防火墙是非简单,只需知道交换机/防火墙的snmp密码,然后连接下自带或导入的snmp模板,就可以完成监控了.比如添加地址为172.10.11.5的交换机监控. 1)登 ...

  4. 【个人阅读】软件工程M1/M2阶段总结

    这次作业是好久以前布置的,由于学期末课程设计任务比较重,我在完善M2阶段的代码的同时又忙于数据库的实现和编译器的实现,一度感觉忙得透不过气来....到这些都基本完成的时候,会看自己以前的阅读心得,觉得 ...

  5. 《Linux内核设计与实现》第4章读书整理

    第四章   进程调度 4.1多任务 无论在单处理器或者多处理机器上,多任务操作系统都能使多个进程处于堵塞或者睡眠状态. 非抢占式多任务:除非进程自己主动停止运行,否则它会一直执行. 抢占式多任务:进程 ...

  6. 01springboot快速入门

    SpringBoot快速入门 springboot的宗旨是习惯大于配置,所以spring里面大量使用了默认的配置来简化spring的配置.spring Boot的主要优点: 为所有Spring开发者更 ...

  7. 第二次sprint

    一.这次冲刺就是实施阶段了,主要对程序进行一次骨架的构建,基本上已经完成了界面的设计,但是在算法上还有很大漏洞,整个程序还是属于不成熟阶段. 二.这是我们的界面 三.已经有的功能:随机生成题目并算出答 ...

  8. shell脚本--内容查找之grep命令

    grep命令可以检索文件中包含关键字(可以使用正则)的行,默认区分大小写. ubuntu@ubuntu:~/test$ cat test.txt this is linux this is Linux ...

  9. PAT 1003 我要通过!

    https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192 “答案正确”是自动判题系统给出的最令人欢喜的 ...

  10. Java与JavaScript 完美实现字符串拆分(利用数组存储)与合并的互逆操作

    Java: String typeStr = "1,2"; String[] typeArray = typeStr.split(","); typeStr = ...