PAT.A1048. Find Coins

原创 2018年04月16日 01:42:44

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 = M and 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

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <iostream>
using namespace std;


int a[100010];
int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	sort(a, a + n);
	int i = 0, j = n-1;
	while (a[i] + a[j] != m&&i<j) {
		if (a[i] + a[j] < m) i++;
		if (a[i] + a[j] > m) j--;

}
	if (i == j) printf("No Solution");
	else printf("%d %d", a[i], a[j]);
	return 0;
	
}

2016高薪Linux培训教程-vim命令和find命令入门到精通

-
  • 1970年01月01日 08:00

【PAT 1068】 Find More Coins 背包问题

1068. Find More Coins (30) 时间限制 150 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard...
  • tiantangrenjian
  • tiantangrenjian
  • 2013-12-15 15:13:25
  • 4187

【PAT】1048. Find Coins (25)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1048 题目描述: Eva loves to collect coins from al...
  • realxuejin
  • realxuejin
  • 2013-08-26 19:44:58
  • 1448

【PAT Advanced Level】1068. Find More Coins (30)

最近在学习动态规划,PAT中的这一题就是一个典型的dp问题。 这一题和01背包问题很类似,M相当于背包问题中的背包容量,硬币面值相当于每件物品的重量,背包问题中要求物品价值最大,这里要求物品总重(面值...
  • gzxcyy
  • gzxcyy
  • 2013-11-01 19:53:17
  • 1248

PAT (Advanced Level) Practise 1068 Find More Coins (30)

1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard...
  • jtjy568805874
  • jtjy568805874
  • 2016-03-22 21:06:31
  • 429

Find More Coins

此题与整数划分为同一类型的题目,基本方法都是搜索+动态规划。         题意是给定总金额,硬币数值,输出恰好能够凑出总金额的硬币组合方法,由于是多解题,要求按字典序最小输出,处理这个字典序只需实...
  • ZJU_fish1996
  • ZJU_fish1996
  • 2016-03-13 14:46:57
  • 1028

1068. Find More Coins (30)-PAT甲级真题(01背包)

1068. Find More Coins (30) Eva loves to collect coins from all over the universe, including som...
  • liuchuo
  • liuchuo
  • 2016-08-22 17:37:42
  • 271

solution Of 1068. Find More Coins (30)

1068. Find More Coins (30) Eva loves to collect coins from all over the universe, including some ot...
  • qq_30490125
  • qq_30490125
  • 2016-04-26 13:25:02
  • 230

PAT-Find Coins (25)

题意: 解答: 经典2-sum,问题,从一串数里找到两个和为m。做法: 把数组排序,然后用两头扫的办法。 i = 0, j = n - 1 如果a[i] + a[j] 如果a[i] + a...
  • zhouyufei0001
  • zhouyufei0001
  • 2016-03-10 10:15:15
  • 154

【C++】PAT(advanced level)1048. Find Coins (25)

1048. Find Coins (25) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN...
  • artemisrj
  • artemisrj
  • 2014-03-09 21:46:18
  • 574
收藏助手
不良信息举报
您举报文章:PAT.A1048. Find Coins
举报原因:
原因补充:

(最多只允许输入30个字)