一道贪心题。
感觉很裸啊,模拟赛时随便乱写了个暴力递归就能过。每次找最接近钱数 \(x\) 的面额 \(num\),如果比钱数少那么答案为剩下 \(x \bmod num\) 钱数的答案加上 \(x \div num\)。否则答案则为剩下 \(num-x\) 钱数的答案加上 \(1\)。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll n, m;
ll a[65];
ll ans(ll x) {
if (x == 0) return 0;
ll minn = 1e18, num;
for (int i = 1; i <= n; i++) {
if (llabs(x - a[i]) < minn) {
minn = llabs(x - a[i]);
num = a[i];
}
}
if (num > x) return ans(num - x) + 1;
return ans(x % num) + x / num;
}
int main() {
ios :: sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
cout << ans(m);
return 0;
}
标签:return,int,题解,ll,long,num,payments,ans,ABC231E
From: https://www.cnblogs.com/xvl-/p/17780905.html