博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1048 Find Coins
阅读量:4540 次
发布时间:2019-06-08

本文共 2635 字,大约阅读时间需要 8 分钟。

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 1 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 (≤, the total number of coins) and M (≤, 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 151 2 8 7 2 4 11 15

Sample Output 1:

4 11

Sample Input 2:

7 141 8 7 2 4 11 15

Sample Output 2:

No Solution
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 int N, M; 7 //放法一,使用窗口函数 8 int main() 9 {10 cin >> N >> M;11 vector
nums(N);12 vector
>res;13 for (int i = 0; i < N; ++i)14 cin >> nums[i];15 sort(nums.begin(), nums.end(), [](int a, int b) { return a < b; });16 int l = 0, r = N - 1, m;17 while (l < r)//找到中间的数刚好与M/2最接近18 {19 m = (l + r) / 2;20 if (nums[m] <= M / 2 && nums[m + 1] > M / 2)21 break;22 if (nums[m] > M / 2)23 r = m - 1;24 else25 l = m + 1;26 }27 r = m + 1;28 l = m;29 while(l>=0 && r
a, pair
b) { return a.first < b.first; });42 if (res.size() == 0)43 cout << "No Solution" << endl;44 else45 cout << res[0].first << " " << res[0].second << endl;46 return 0;47 }48 49 //方法二,使用数找数原理50 int main()51 {52 cin >> N >> M;53 int nums[1001], t;//根据题目要新建的数组大小54 memset(nums, 0, sizeof(int) * 1001);55 for (int i = 0; i < N; ++i)56 {57 cin >> t;58 nums[t]++;//统计个数59 }60 for (int i = 0; i < 1001; ++i)61 {62 if (nums[i] > 0)//个数大于063 {64 nums[i]--;//使用掉一个数字65 if (M > i && nums[M - i] > 0)66 {67 cout << i << " " << M - i << endl;//由最小值开始遍历,故为最优答案68 return 0;69 }70 nums[i]++;//没有使用,还回去71 }72 }73 cout << "No Solution" << endl;74 return 0;75 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11275777.html

你可能感兴趣的文章
flask-sqlalchemy(包含离线脚本,with在上下文管理的应用)
查看>>
机器学习工程师 - Udacity 强化学习 Part Ten
查看>>
go语言 新手学习笔记 go基础教程
查看>>
zabbix 添加宏变量
查看>>
2016年11月1日——jQuery源码学习笔记
查看>>
Thinkphp5笔记二:创建模块
查看>>
centos 安装mysql
查看>>
Redis 禁用FLUSHALL FLUSHDB KEYS 命令
查看>>
Matlab中imread函数使用报错“不应为MATLAB 表达式”分析
查看>>
MFC ADO数据库操作
查看>>
图像质量评价-NQM和WPSNR
查看>>
面试准备——相关知识
查看>>
每日一字:悟
查看>>
CentOS7.6安装稳定版Nginx
查看>>
LeetCode 1002. Find Common Characters (查找常用字符)
查看>>
建立隐藏管理员用户
查看>>
android设置图文提醒功能
查看>>
ajax跨域提交
查看>>
完成登录与注册页面的前端
查看>>
Mac下source tree 下的安装
查看>>