PAT A1048 Find Coins

PAT A1048 Find Coins

题目描述:

  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 10​5​​ 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 (≤10​5​​, the total number of coins) and M (≤10​3​​, 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 V​1​​ and V​2​​ (separated by a space) such that V​1​​+V​2​​=M and V​1​​≤V​2​​. If such a solution is not unique, output the one with the smallest V​1​​. 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:采用Hash散列的方法:

 1 /****************************************************
 2 PAT A1048 Find Coins
 3 ****************************************************/
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <vector>
 7 
 8 using namespace std;
 9 
10 int main() {
11     int CoinsCnt = 0, payments = 0;
12 
13     cin >> CoinsCnt >> payments;
14 
15     vector<int> Hash(payments, 0);
16     vector<int> Coins(CoinsCnt, 0);
17 
18     for (int i = 0; i < CoinsCnt; ++i) {
19         cin >> Coins[i];
20 
21         //因为要求用两枚硬币支付,故舍去面值大于支付总额的货币信息以节约内存
22         if (Coins[i] < payments) Hash[Coins[i]]++;
23     }
24 
25     sort(Coins.begin(), Coins.end());
26 
27     for (int i = 0; i < CoinsCnt && Coins[i] < payments; ++i) {
28         if (Coins[i] != payments - Coins[i] && Hash[payments - Coins[i]] >= 1 ||
29             Coins[i] == payments - Coins[i] && Hash[payments - Coins[i]] >= 2) {
30             cout << Coins[i] << ' ' << payments - Coins[i];
31             return 0;
32         }
33     }
34 
35     cout << "No Solution";
36 
37     return 0;
38 }

  2:采用Two pointers的方法:

 1 /****************************************************
 2 PAT A1048 Find Coins
 3 ****************************************************/
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <vector>
 7 
 8 using namespace std;
 9 
10 int main() {
11     int CoinsCnt = 0, payments = 0;
12 
13     cin >> CoinsCnt >> payments;
14 
15     vector<int> Coins(CoinsCnt, 0);
16 
17     for (int i = 0; i < CoinsCnt; ++i) {
18         cin >> Coins[i];
19     }
20 
21     sort(Coins.begin(), Coins.end());
22 
23     int beg = 0, end = CoinsCnt - 1;
24     while (beg < end) {
25         if (Coins[beg] + Coins[end] == payments) {
26             cout << Coins[beg] << ' ' << Coins[end];
27             return 0;
28         }
29         else if (Coins[beg] + Coins[end] > payments) end--;
30         else if (Coins[beg] + Coins[end] < payments) beg++;
31     }
32 
33     cout << "No Solution";
34 
35     return 0;
36 }

注意事项:

  1:第一种方法中如果coin的面值过大会导致Hash容器占用大量的内存,又因为题目中要求使用两枚硬币付款,也就是说每一枚硬币的面值只能小于需要支付的金额(即payments),所以对面值大于等于payments的硬币可以不做处理,Hash容器的大小可以设置为payments,从而减少内存的使用。

  2:Two Pointers实际上是在有序容器的前后设置一个“指针”,当所指向的两个元素的和大于payments时,令后一个指针前移从而减小所指两个元素的和;当所指向的两个元素的和小于payments时,令前一个指针前移从而增大所指两个元素的和;当所指向的两个元素的和等于payments时,这两个指针指向的元素就是我们要寻找的结果;当两个指针相等时依然没有可行的输出,那就说明“No Solution"。

  3:给定一个数组和一个常量,求数组中那三个元素之和等于这个常量的问题也可以使用第二种方法的思路。此处暂时想不起这个题实在那个网站的题库里的,后期找到的话在该文章下附上链接。

posted @ 2019-10-12 22:27  多半是条废龙  阅读(293)  评论(0编辑  收藏  举报