1048 Find Coins (25 分)

360 篇文章 3 订阅
91 篇文章 1 订阅

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
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 , the total number of coins) and M (≤10
, 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.
. 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

结尾无空行

简单双指针阿,直接排序然后双指针就出来了
不解释了

#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<cctype>
#include<algorithm>
#include<stack>
using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	int a[n];
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);
	int i = 0, j = n - 1;
	bool flag = false;
	while (i < j) {
		if (a[i] + a[j] == k) {
			flag = true;
			cout << a[i] << " " << a[j];
			break;
		} else if (a[i] + a[j] > k) {
			j--;
		} else {
			i++;
		}
	}
	if (!flag) {
		cout << "No Solution";
	}
	return 0;
}

在Python中,我们可以使用二查找算法的思想来处理找零问题,特别是在找零硬币的场景中。所谓的“三法”实际上是一种优化过的二查找,这里假设我们有一个列表表示可用的面额,例如[1, 3, 5, 10],而我们需要找到最小的组合来凑出某个金额。以下是基本步骤: 1. **初始化**: - 定义一个函数`find_smallest_coins`,它接受目标金额`target`和硬币面额列表`coins`作为参数。 - 初始化两个指针`low`和`high`,别指向列表的第一个元素和最后一个元素。 2. **递归过程**: - 检查`low`是否小于等于`high`。 - 如果是,计算中间位置`mid`。 - 如果`coins[mid]`正好等于`target`,返回`[coins[mid]]`。 - 如果`coins[mid]`大于`target`,说明最小面额应该在前半部,将`high`设为`mid - 1`。 - 否则,如果`coins[mid]`小于`target`,说明最小面额应该在后半部,将`low`设为`mid + 1`。 3. **递归结束条件**: - 当`low`大于 `high`时,意味着找不到合适的组合,返回空列表`[]`,或者可以返回一个默认提示如"无法凑出目标金额"。 4. **返回结果**: - 函数返回找到的最小面额组合列表。 下面是一个简单的示例代码实现: ```python def find_smallest_coins(target, coins): if not coins or target < coins[0]: return [] low, high = 0, len(coins) - 1 while low <= high: mid = (low + high) // 2 if coins[mid] == target: return [coins[mid]] elif coins[mid] > target: high = mid - 1 else: low = mid + 1 # 当无法找到合适组合时返回默认信息 return "无法凑出目标金额" # 示例 coins = [1, 3, 5, 10] print(find_smallest_coins(11, coins)) # 输出: [1, 1, 1, 1, 1] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_努力努力再努力_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值