1048 Find Coins (25 分)
 

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

题意:

假设你有N个货币,面值从1到500不等,只允许使用其中的两枚货币,然后刚好达到付款要求,如果存在多组方案,输出其中一个货币最小的方案。如果没有符合要求的方案,输出No Solution。

题解:

货币的面值只在1~500之间,那么使用一个500的int 数组存储每个面额的张数即可。

采用空间的方法,直接看另一个数是否存在。注意可能存在2个重复的数,所以要计数,而不是用bool表示。

这道题的测试点有个bug,题目明明说的是面值500,但是第三个测试点,给出了面值999的测试,所以数组不能只开500多。

AC代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int v[];//505会出现段错误
int main(){
int n,m,x;
memset(v,,sizeof(v));
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>x;
v[x]++;
}
int f=;
for(int i=;i<=;i++){
if(i!=m-i){
if(v[i]&&v[m-i]){
f=;
cout<<i<<" "<<m-i;
break;
}
}else if(i==m-i&&v[i]>=){
f=;
cout<<i<<" "<<m-i;
break;
}
}
if(!f) cout<<"No Solution";
return ;
}

最新文章

  1. LSOF 安装与使用
  2. cxf和spring结合
  3. 测试你是否和LTC水平一样高[HDU1407]
  4. warning C4305: “=”: 从“int”到“unsigned char”截断解决方法[zz]
  5. 奇怪的margin,padding,table
  6. C++中const用法总结
  7. DOCTYPE对$(window).height()取值的影响
  8. C#中使用SendMessage进行进程通信的实例
  9. 具体解释HTML中的window对象和document对象
  10. OSS.Social微信项目标准库介绍
  11. flutter 新增安卓ios 闪图
  12. linux使用npm成功安装命令后,执行时却报找不到命令的问题
  13. LODOP打印控件进行批量打印
  14. [转]C#利用反射实现两个类的对象之间相同属性的值的复制
  15. Minimum Cost POJ - 2516 (模板题 spfa最小费用最大流)
  16. Unicode String to a UTF-8 TypedArray Buffer in JavaScript
  17. threejs 世界坐标转化为屏幕坐标
  18. elasticsearch系列二:索引详解(快速入门、索引管理、映射详解、索引别名)
  19. JavaEE Cookie HttpSession 学习笔记
  20. 百度 echarts K线图使用

热门文章

  1. How to parse unix timestamp to time.Time
  2. PL/SQL复合类型
  3. unable to import maven project see logs for details
  4. tcp文件下载
  5. 2019-2020-1 20199312《Linux内核原理与分析》第一周作业
  6. BUG----spark
  7. HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9
  8. PostgreSQL 数据目录结构
  9. 后缀数组 1031: [JSOI2007]字符加密Cipher
  10. 【原创】go语言学习(十六)接口