1048 Find Coins (25分)
codeflysafe Lv5

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 (≤, 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 and
​​ (separated by a space) such that and . If such a solution is not unique, output the one with the smallest . If there is no solution, output No Solution instead.

Sample

Input 1:

1
2
8 15
1 2 8 7 2 4 11 15

Output 1:

1
4 11

Input 2:

1
2
7 14
1 8 7 2 4 11 15

Output 2:

1
No Solution

Solution

参见 leetcode 的 Two-Sum,入门级问题

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<cmath>

using namespace std;


void pat_1048(){
int n,m;
cin >> n >> m;
int maps[m];
fill(maps,maps+m,0);
int v1 = m,coin;
for(int i=0;i<n;i++){
cin >> coin;
if(m > coin){
if(maps[coin]==1){
v1 = min(coin,min(m-coin,v1));
}else maps[m-coin] = 1;
}
}
if( v1 != m ) cout << v1 << " " << m-v1;
else cout << "No Solution" << endl;

}

int main(){
pat_1048();
return 0;
}
  • 本文标题:1048 Find Coins (25分)
  • 本文作者:codeflysafe
  • 创建时间:2020-03-22 15:29:19
  • 本文链接:https://codeflysafe.github.io/2020/03/22/2020-03-22-1048-Find-Coins-(25分)/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论