【算法学习记录-散列】【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 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 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、题解代码

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int N = 1005;
 5 int hashTable[N];
 6 int main() {
 7     
 8     int n, m, num;
 9     scanf("%d %d", &n, &m);
10     for (int i = 0; i < n; i++) {
11         scanf("%d", &num);
12         hashTable[num]++;
13     }
14     
15     for (int i = 0; i < N; i++) {
16         if (hashTable[i] && hashTable[m - i]) {
17             if (i == m - i && hashTable[i] <= 1) {
18                 continue;
19             }
20             printf("%d %d\n", i, m - i);
21             return 0;
22         }
23     }
24     
25     printf("No Solution\n");
26     return 0;
27     
28 }

2、一些细节

①i == m - i时需要关注相同的数字是否有两个

②PAT这题的测试点2不知道怎么设计的??????如有知道的麻烦告知

上一篇:前端学习(2856):简单秒杀系统学习之定时器循环显示


下一篇:必备(20)