题解 | #最小邮票数#
最小邮票数
https://www.nowcoder.com/practice/83800ae3292b4256b7349ded5f178dd1
#include <iostream> #include<algorithm> #include<vector> using namespace std; int main() { int number; while (cin >> number) { // 注意 while 处理多个 case int n; cin >> n; vector<int> weights(n); for (int i = 0; i < n; i++) { cin >> weights[i]; } vector<int> dp(number + 1, n + 1); dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = number; j >= weights[i]; j--) { dp[j] = min(dp[j], dp[j - weights[i]] + 1); } } if (dp[number] == n + 1) cout << 0 << endl; else cout << dp[number] << endl; } } // 64 位输出请用 printf("%lld")