PAT A 甲级 1048 Find Coins (25分)

发布于:2021-10-25 07:26:03

1048 Find Coins (25分)
吐槽

从头文件里强行摸出了自己没背住的结构名字。还行还行。


思路

看上去有超时风险。大概吧。
总之我用的是set。
自排序+find函数,简直不能更适合。


注意:测点2中出现了所求结果是两个同值的硬币的情况。由于set不保存重复数字,所以需要用multiset。同时,find函数只能找到第一个所求解,所幸排序了之后如果有同值硬币,那所find的位置的下一个应该为同值;反之则是只有一枚所需价钱一半的硬币,不予作为solution输出。


代码

#include
#include
#include

using namespace std;

int main()
{
int n, m, i, temp;
cin >> n >> m;
set coins;
for (i = 0; i < n; i++)
{
cin >> temp;
coins.insert(temp);
}
set::iterator it, it2;
for (it = coins.begin(); it != coins.end(); it++)
{
it2 = coins.find(m - *it);
if (it2 != coins.end())
{
if (it2 == it)
{
it2++;
if (*it2 != *it)
continue;
}
cout << *it << " " << *it2;
return 0;
}
}
cout << "No Solution";
return 0;
}

题目

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

相关推荐

最新更新

猜你喜欢