【PAT】1048. Find Coins (25)

发布时间:2017-6-23 9:44:11 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"【PAT】1048. Find Coins (25)",主要涉及到【PAT】1048. Find Coins (25)方面的内容,对于【PAT】1048. Find Coins (25)感兴趣的同学可以参考一下。

题目链接: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 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 分析:(1)用查询表,即 input[i]  标记数i出现的次数。(2)要考虑M等于某个数的两倍的情况。比如14 = 7 + 7。 #include<iostream> #include<string.h> using namespace std; #define max 100000 int 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:[email protected]/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 1000 int main() { int n, m; while(scanf("%d%d",&n,&m)!=EOF) { //input std::vector<int> coins; coins.assign(FaceMax+1, 0); while(n--) { int tmp; scanf("%d",&tmp); coins[tmp]++; } //search bool 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 solution if(!flag) printf("No Solution\n"); } }

上一篇:日志20130104~0308
下一篇:【JAVA教程】第六讲 Java的线程和Java Applet

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。