49 lines
1.1 KiB
C++
49 lines
1.1 KiB
C++
#include <iostream>
|
||
|
||
using namespace std;
|
||
|
||
//一分
|
||
#define ONEFEN 1
|
||
//五分
|
||
#define FIVEFEN 5
|
||
//十分
|
||
#define TENFEN 10
|
||
//二十五分
|
||
#define TWENTYFINEFEN 25
|
||
|
||
//分硬币问题使用贪心策略是存在问题的,因为参考:
|
||
// https://mp.weixin.qq.com/s/978Tdplj3IaSG2dc-5F-aw
|
||
// 现在问题变了,还是需要找给顾客41分钱,现在的货币只有 25 分、20分、10 分、5 分和 1 分四种硬币;该怎么办?
|
||
|
||
int main() {
|
||
//总和41
|
||
int sum_money = 41;
|
||
//每个多少个?
|
||
int num_25 = 0, num_10 = 0, num_5 = 0, num_1 = 0;
|
||
|
||
//不断尝试每一种硬币
|
||
while (sum_money >= TWENTYFINEFEN) {
|
||
num_25++;
|
||
sum_money -= TWENTYFINEFEN;
|
||
}
|
||
while (sum_money >= TENFEN) {
|
||
num_10++;
|
||
sum_money -= TENFEN;
|
||
}
|
||
while (sum_money >= FIVEFEN) {
|
||
num_5++;
|
||
sum_money -= FIVEFEN;
|
||
}
|
||
while (sum_money >= ONEFEN) {
|
||
num_1++;
|
||
sum_money -= ONEFEN;
|
||
}
|
||
|
||
//输出结果
|
||
cout << "25分硬币数:" << num_25 << endl;
|
||
cout << "10分硬币数:" << num_10 << endl;
|
||
cout << "5分硬币数:" << num_5 << endl;
|
||
cout << "1分硬币数:" << num_1 << endl;
|
||
|
||
return 0;
|
||
} |