A1048. Find Coins
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的更多相关文章
- PAT甲级——A1048 Find Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- A1048 Find Coins (25 分)
一.技术总结 首先初看题目有点没读懂,题目大致意思是小明有很多个硬币不同面值的,但是现在他要到商家这里换新的面值, 且商家有一个规定,一个新的硬币必须要你两个硬币面值相加等于的来换,这一有第一个问题产 ...
- PAT_A1048#Find Coins
Source: PAT A1048 Find Coins (25 分) Description: Eva loves to collect coins from all over the univer ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 1048 Find Coins (25 分)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...
- [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 ...
- 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 ...
- 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 ...
- csuoj 1119: Collecting Coins
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec Memo ...
随机推荐
- C# 根据部分属性来判断俩个对象是否相同
根据部分属性来判断俩个对象是否相同 代码是第一版本 可能不牢固 有问题请反馈一下 3QU 效果图: public static class CustomExpand { public static b ...
- Vue2.0 搭配 axios
1.安装axios $ npm install axios 2.Demo (1)Get // 为给定 ID 的 user 创建请求 axios.get('/user?ID=12345') .then( ...
- ARM-GPIO
操作GPIO有三种方法: 调用库函数读取IO的输入电平:uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx,uint16_t GPIO_pin): 操作寄 ...
- Java Mongo 自定义序列化笔记
从insert方法入手 1. org.springframework.data.mongodb.repository.support.SimpleMongoRepository.java inse ...
- Lotto HDU
链接 [http://acm.hdu.edu.cn/showproblem.php?pid=1342] 题意 分析 DFS 代码 #include<cstdio> #include< ...
- S2X环境搭建与示例运行
S2X环境搭建与示例运行 http://dbis.informatik.uni-freiburg.de/forschung/projekte/DiPoS/S2X.html 环境 Maven proje ...
- Linux内核分析——第三章 进程管理
第三章 进程管理 3.1 进程 1.进程就是处于执行期的程序:进程就是正在执行的程序代码的实时结果:进程是处于执行期的程序以及相关的资源的总称:进程包括代码段和其他资源. 线程:是在进程中活动的对象. ...
- SQLSERVER最简单的同名数据库恢复过程.
一. 冷备份恢复 1. net stop mssqlserver # 如果是安装的默认数据库实例 关闭 sqlserver的数据库 2. copy sqlserver的数据文件 主要是mdf 数据文件 ...
- Linux 检查磁盘性能速度
1. hdparm 工具: hdparm –t 设备名(/dev/sda1) 2. time dd if=/dev/zero of=/tmp/test.dat bs=1G count=1
- Node buffer模块缓冲区
//1:字节 byte//1024b = 1kb [千字节]//1024kb = 1mb [兆字节]//1024mb = 1gb [吉字节]//1024gb = 1tb [梯]//1024tb = 1 ...