题目传送门
一道贪心题……
感觉很裸啊,模拟赛时随便乱写了个暴力递归就能过。每次找最接近钱数 x 的面额 num,如果比钱数少那么答案为剩下 xmodnum 钱数的答案加上 x÷num。否则答案则为剩下 num−x 钱数的答案加上 1。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #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; }
|