标签:11 Coins PAT nums int coins she include Find
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 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 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 #include <cstdlib> 5 using namespace std; 6 int N, M; 7 //放法一,使用窗口函数 8 int main() 9 { 10 cin >> N >> M; 11 vector<int>nums(N); 12 vector<pair<int, int>>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 else 25 l = m + 1; 26 } 27 r = m + 1; 28 l = m; 29 while(l>=0 && r<N)//然后使用窗口函数,进行左右移动 30 { 31 if (nums[l] + nums[r] == M) 32 { 33 res.push_back(make_pair(nums[l], nums[r])); 34 r++; 35 } 36 else if (nums[l] + nums[r] < M) 37 r++; 38 else 39 l--; 40 } 41 sort(res.begin(), res.end(), [](pair<int, int> a, pair<int, int> b) {return a.first < b.first; }); 42 if (res.size() == 0) 43 cout << "No Solution" << endl; 44 else 45 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)//个数大于0 63 { 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 }
标签:11,Coins,PAT,nums,int,coins,she,include,Find 来源: https://www.cnblogs.com/zzw1024/p/11275777.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。