GESP C++ 真题 · 逐题精解
首页C++五级真题 › 2025年6月 › 第13题

GESP 2025年6月 C++五级 单选题 第13题

C++五级单选题2025年6月第13题

所属知识点:贪心算法 难度要求:掌握 考频:—

13.硬币找零问题中要求找给客户最少的硬币。 coins 存储可 硬币规格,单位为角,假设规格都 于10 用 小 角,且 定有1角规格。 amount 为要找零的 额,约定必须为1角的整数倍。输出为每种规格及其数量,按规格从 一 金 大 到 输出,如果某种规格不必要,则输出为0。下 是其实现代码,相关说法正确的是( )。 小 面
const int MAX_COINS = 10;
int result[MAX_COINS] = {0};     // 假设最多10种面额
int find_coins(const vector<int>& coins, int amount) {
sort(coins.begin(), coins.end(), greater<int>());
int n = coins.size();
for (int i = 0; i < n; ++i) {
int coin = coins[i];
int num = amount / coin;
result[i] = num;
amount -= num * coin;
if (amount == 0) break;
}
cout << "找零方案如下:" << endl;
for (int i = 0; i < n; ++i) {
cout << sorted_coins[i] << "角需要" << result[i] << "枚" << endl;
}
return 0;
}

正确答案:A

题目解析

硬币找零按面额从大到小尽量多取,采用的是贪心算法,选 A。……

完整解析为会员内容二级及以上的逐题精讲需开通 VIP。一级解析全部免费。前往 GESPPASS 解锁

想系统刷完 GESP C++ 1~8 级真题,并查看每道题的逐题精讲?

进入 GESPPASS 开始练习