题解 | #兑换零钱#
兑换零钱
https://www.nowcoder.com/practice/67b93e5d5b85442eb950b89c8b77bc72
#include <algorithm> #include <cstring> #include <iostream> using namespace std; const int N = 10010; int f[N], a[N]; int main() { int n, m; cin >> n >> m; for(int i = 1; i <= n; i ++) cin >> a[i]; memset(f, 0x3f, sizeof f); f[0] = 0; for(int i = 1; i <= n; i ++) for(int j = a[i]; j <= m; j ++) f[j] = min(f[j], f[j - a[i]] + 1); if(f[m] > 5000) cout << "-1" << endl; else cout << f[m] << endl; return 0; }