【PAT】A1048. Find Coins (25)

Description:
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

hash法:

//NKW 甲级真题1037
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int maxn = 100010;
int hash[maxn];
int main(){
	memset(hash, 0, sizeof(hash));
	int n, m, num, flag = 0;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++){
		scanf("%d", &num);
		hash[num]++;
	}
	for (int i = 0; i < m; i++){
		if (hash[i] && hash[m - i]){
			if (i == m - i&&hash[i] <= 1)		//2*x=m,然而x只有一个
				continue;
			printf("%d %d\n", i, m - i);
			flag = 1;
			break;
		}
	}
	if (!flag)
		printf("No Solution\n");
	system("pause");
	return 0;
}

把Solution拼成Solutin是非常蛋疼的。再见

二分法:

//NKW 甲级真题1037
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int num[maxn];
int bisearch(int left, int right, int x){
	int mid;
	while (left <= right){
		mid = (left + right) / 2;
		if (num[mid] == x)	return mid;
		else if (num[mid] < x)	left = mid + 1;
		else	right = mid - 1;
	}
	return -1;
}
int main(){
	int n, m, flag = 0;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%d", &num[i]);
	sort(num, num + n);
	for (int i = 0; i < n; i++){
		int pos = bisearch(0, n - 1, m - num[i]);
		if (pos != -1 && pos != i){
			printf("%d %d\n", num[i], num[pos]);
			flag = 1;
			break;
		}
	}
	if (!flag)
		printf("No Solution\n");
	system("pause");
	return 0;
}

two pointers法:

//NKW 甲级真题1037
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 100010;
bool cmp(int a, int b){
	return a < b;
}
int num[maxn];
int main(){
	int n, m, flag = 0;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%d", &num[i]);
	sort(num, num + n, cmp);
	int i = 0, j = n - 1;
	while (i != j){
		if (num[i] + num[j]>m)
			j--;
		else if (num[i] + num[j] < m)
			i++;
		else{
			printf("%d %d\n", num[i], num[j]);
			flag = 1;
			break;
		}
	}
	if (!flag)
		printf("No Solution\n");
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ztmajor/article/details/80949847