PAT 1048 Find Coins[比较]
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 105 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 (≤105, the total number of coins) and M (≤103, 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 V1 and V2 (separated by a space) such that V1+V2=Mand V1≤V2. If such a solution is not unique, output the one with the smallest V1. 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个数,并给出一个两个数的和,判断是否存在这样的两个数,如果有相同的,输出V1较小的结果。
#include <iostream>
#include <algorithm>
#include <vector>
#include<string.h>
#include<string>
#include<cstdio>
using namespace std; int a[];
int main()
{
int n,total;
cin>>n>>total;
for(int i=;i<n;i++){
cin>>a[i];
}
int f=-,s=-;
sort(a,a+n);//从小到大排列。
bool flag=true;
for(int i=;i<n-&&flag;i++){
for(int j=i+;j<n;j++){
if(a[i]+a[j]==total){
f=a[i];
s=a[j];
flag=false;break;
}else if(a[i]+a[j]>total)break;
}
}
if(f!=-&&s!=-)
cout<<f<<" "<<s;
else
cout<<"No Solution";
return ;
}
//一开始写成这个样子,牛客网上通过60%,PAT上有两个测试点没通过,都是因为运行超时,不知如何解决。
下是柳神的解答,实在是叹为观止。
1.使用数组记录数字出现的个数,
2硬币面值不超过500!!!。。
PAT 1048 Find Coins[比较]的更多相关文章
- PAT 1048. Find Coins
two sum题目,算是贪婪吧 #include <cstdio> #include <cstdlib> #include <vector> #include &l ...
- PAT 解题报告 1048. Find Coins (25)
1048. Find Coins (25) Eva loves to collect coins from all over the universe, including some other pl ...
- PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏
1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- 浙大pat 1048 题解
1048. Find Coins (25) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- 1048 Find Coins (25 分)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...
- PAT 甲级 1048 Find Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...
- PAT Advanced 1048 Find Coins (25 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- PAT Advanced 1048 Find Coins (25) [Hash散列]
题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...
随机推荐
- kettle利用触发器实现数据同步
2016年8月17日 一.目的 通过触发器实现数据同步二.思路 1.在数据库需要同步的源表中建立一个insert触发器,当有新数据插入时,会自动将新插入数据的主键记录到临时表temp中.(当然也可以记 ...
- CI框架中 类名不能以方法名相同
昨天晚上一个坑爹的问题折腾了我一晚上,首先我来说下我的代码,我建立了一个index的控制器然后呢 在控制器里有一个index的方法.页面模板都有. if ( ! defined('BASEPATH' ...
- [android] AndroidManifest.xml【 manifest -> uses-permission】
在 API Level 1 时被引入 简介: 在某些情况下,你为app设置的权限将会影响到google应用商店会用何种规则来过滤你的APP. 如果你需要一个硬件相关的权限——CAMERA,googl ...
- C++ 类的抽象初练
/* 某商店经销一种货物,货物的购进和卖出以箱为单位,各箱的重量不一样, 因此商店需要目前库存的总重量. 现在用c++模拟商店货物购进和卖出的情况 */ #include<iostream> ...
- php url配置项
- uva 11381(神奇的构图、最小费用最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=29821 思路:首先拆点,每个字符对应的位置拆成i和i+len,然后 ...
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- webstorm配置内存参数,解决卡顿
找到WebStorm.exe.vmoptions这个文件,路径如下webstorm安装主目录>bin>WebStorm.exe.vmoptions更改为第二行:-Xms526m第三行:-X ...
- 《C++ Primer Plus》第5章 循环和关系表达式 学习笔记
C++提供了3种循环: for 循环. while 循环 和 do while 循环 .如果循环测试条件为 true 或非零,则循环将重复执行一组指令: 如果测试条件为 false 或 0 , 则结束 ...
- 如何用sql语句复制一张表
如何用sql语句复制一张表 1.复制表结构及数据到新表 CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete ...