大面值优先找 63 要几枚
面值有 25、10、5、1 四种,要找 63。每次都拿不超过剩下金额的最大面值。
运行下面这段程序:
#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 main() {
cout << greedy({25, 10, 5, 1}, 63) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)