题目链接: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 15Sample Output 1:
4 11Sample Input 2:
7 14 1 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 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://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; }
#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"); } }