这组面值上贪心和最优一样吗
找 63,一边用贪心,一边用 DP 算出的最优解。
运行下面这段程序:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int greedy(const vector<int>& cs, int t) {
// 面值已按从大到小给好:每次拿不超过剩下金额的最大面值
int n = 0;
for (int c : cs) {
while (t >= c) {
t -= c;
n++;
}
}
return n;
}
int best(const vector<int>& cs, int t) {
// dp[a] = 凑够 a 最少要几枚
const int INF = 1000000000;
vector<int> dp(t + 1, INF);
dp[0] = 0;
for (int a = 1; a <= t; a++) {
for (int c : cs) {
if (c <= a && dp[a - c] + 1 < dp[a]) dp[a] = dp[a - c] + 1;
}
}
return dp[t];
}
int main() {
cout << greedy({25, 10, 5, 1}, 63) << "/" << best({25, 10, 5, 1}, 63) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)