Banknotes

链接

题目链接

题意

有 n 种货币,问你对于给定的整数 k,找出不能用 k 或更少的钞票表示的最小正数 s

思路

因为是求对于给定的整数 k,找出不能用 k 或更少的钞票表示的最小正数 s,所以要尽可能使用多的小面值货币,什么叫尽可能多呢?尽可能多就是指除了最大面值的货币外,其余货币与比他大一点的面值的货币之差,比如有 2 种面值 $1 和 $10,尽可能多的使用 $1 货币,这里的尽可能多就是最多使用 9 张 $1 面值的货币,因为使用 10 张得到的面值即为 10,但这个面值可以只用一张 $10 的货币即可达到,因此最多只能使用 9 张 $1 面值的货币

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <cmath>
typedef long long ll;
using namespace std;
#define ioClose() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'

const int maxn = 1e9 + 5;
const int mod = 998244353;
// 货币的面值,因为题目规定 n 不超过 10,所以面值最大为 10e10,可以用 long long 存储
ll type[15];
// cnt[i]: 最多使用 type[i] 面值的货币的数量
ll cnt[15];
int main() {
	ioClose();
	int t;
	cin >> t;
	while (t--) {
		int n, k;
		cin >> n >> k;
		for (int i = 0; i < n; i++) {
			int temp;
			cin >> temp;
			type[i] = pow(10, temp);
			if (i != 0) {
				// type[i] - type[i - 1]:相邻 2 种货币的面值差
				// (type[i] - type[i - 1]) / type[i - 1]:最多使用 type[i - 1] 面值的货币的数量
				cnt[i - 1] = (type[i] - type[i - 1]) / type[i - 1];
			}
		}
		ll ans = 0;
		// 在该循环中,k 代表还需要使用的货币数,ans 代表当前价值
		for (int i = 0; i < n - 1; i++) {
			if (k > cnt[i]) {
				k -= cnt[i];
				ans += type[i] * cnt[i];
			} else if (k == cnt[i]) {
				k -= cnt[i];
				ans += type[i] * cnt[i];
				ans += type[i + 1];
				break;
			} else {
				ans += type[i] * (k + 1);
				k = 0;
				break;
			}
		}
		// 如果在使用除了最大面值的货币的其余货币之后仍未达到要求,则剩下的全部使用最大面值的货币
		if (k > 0) ans += type[n - 1] * (k + 1);
		cout << ans << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值