【PAT】1048. Find Coins (25)
来源:互联网 发布:快彩网彩票php源码 编辑:程序博客网 时间:2024/05/22 16:04
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1048
题目描述:
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 151 2 8 7 2 4 11 15Sample Output 1:
4 11Sample Input 2:
7 141 8 7 2 4 11 15Sample Output 2:
No Solution
分析:(1)用查询表,即 input[i] 标记数i出现的次数。(2)要考虑M等于某个数的两倍的情况。比如14 = 7 + 7。
#include<iostream>#include<string.h>using namespace std;#define max 100000int input[max];int main(){int N,M;cin>>N>>M;int i,j;int temp;int flag;bool find = false;memset(input,0,sizeof(input));for(i=0; i<N; i++){cin>>temp;input[temp] ++;}for(i=0; i<=M/2; i++){//以下两行是为了判断是否存在两个同样的数和为题目所求,比如14=7+7;input[i]--;input[M-i]--;if(input[i]>=0 && input[M-i]>=0){find = true;cout<<i<<" "<<M-i<<endl;break;}}if( !find )cout<<"No Solution"<<endl;return 0;}
另附其他的一些参考代码。
(1)http://blog.163.com/hgfeaon@126/blog/static/95856212013119113055361/
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n,m; cin>>n>>m; vector<int> coins(n); while (n--) { cin>>coins[n]; } sort(coins.begin(), coins.end()); vector<int>::iterator iter; for(iter = coins.begin(); iter != coins.end(); iter++) { int other = m - *iter; if (binary_search(iter + 1, coins.end(), other)) { cout<<*iter<<" "<<other<<endl; break; } } if (iter == coins.end()) { cout<<"No Solution"<<endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, m; cin>>n>>m; vector<int> coins(n); while (n--) { cin>>coins[n]; } sort(coins.begin(), coins.end()); int i = 0; int j = coins.size() - 1; while(i < j) { int value = coins[i] + coins[j]; if (value > m) { j--; } else if (value < m) { i++; } else { break; } } if (i < j) { cout<<coins[i]<<" "<<coins[j]<<endl; } else { cout<<"No Solution"<<endl; } return 0; }
(2)http://blog.csdn.net/sunbaigui/article/details/8656978
#include<iostream>#include<vector>#define FaceMax 1000int main(){int n, m;while(scanf("%d%d",&n,&m)!=EOF){//inputstd::vector<int> coins;coins.assign(FaceMax+1, 0);while(n--){int tmp;scanf("%d",&tmp);coins[tmp]++;}//searchbool flag = false;for(int i = 1; i <= m/2; ++i){coins[i]--;coins[m-i]--;if(coins[i]>=0 && coins[m-i] >= 0){printf("%d %d\n",i,m-i);flag = true;break;}}//no solutionif(!flag)printf("No Solution\n");}}
- 1048. Find Coins (25)-PAT
- 【PAT】1048. Find Coins (25)
- PAT 1048. Find Coins (25)
- pat 1048. Find Coins (25)
- PAT 1048. Find Coins (25)
- PAT 1048. Find Coins (25)
- pat 1048. Find Coins (25)
- PAT 1048.Find Coins
- 【PAT】1048. Find Coins
- PAT--1048. Find Coins
- PAT 1048. Find Coins
- PAT-Find Coins (25)
- PAT (Advanced) 1048. Find Coins (25)
- PAT甲 1048. Find Coins (25)
- PAT(A) - 1048. Find Coins (25)
- 【PAT甲级】1048. Find Coins (25)
- 1048. Find Coins (25) PAT 甲级
- PAT(甲级)1048. Find Coins (25)
- SharePoint 2010 最佳实践学习总结------第2章 SharePoint Windows PowerShell指南
- 抽象类的构造方法
- Win 7下Chrome字体渲染颜色太淡的解决方法
- 日志20130104~0308
- Opencv 兼容OPENCV1.0
- 【PAT】1048. Find Coins (25)
- c语言中volatile关键字的作用
- 括号配对问题 栈的使用
- 趣文:计算机网络基本概念趣解
- Linux下wget命令详解
- Yii 使用gmail发送邮件
- 算法优化,如何从120秒到0.5秒【数据结构的选择、数据类型的选择、运算优先级的选择、函数调用关系】
- 日志20130325~0614
- 小经验之