简单贪心算法问题--找零钱

来源:互联网 发布:高中教学视频 淘宝网 编辑:程序博客网 时间:2024/06/05 02:49

问题:

很简单,先找面值大的。

#include<iostream>using namespace std;void find_change(int total_change){    int Q, D, N, P;    Q = D = N = P = 0;    if ((total_change / 25 > 0) && (total_change > 10))    {        Q = total_change / 25;    }    int total_change_D = total_change % 25;    if ((total_change_D / 10 > 0) && (total_change > 5))    {        D = total_change_D / 10;    }    int total_change_N = total_change_D % 10;    if ((total_change_N / 5 > 0) && (total_change_D > 1))    {        N = total_change_N / 5;    }    P = total_change_N % 5;    cout << "quarters Q = " << Q << endl;    cout << "dimes    D = " << D << endl;    cout << "nickles  N = " << N << endl;    cout << "pennies  P = " << P << endl;}int main(){    int amount = 0;    cout << "Please input the changes you need: " << endl;    cin >> amount;    find_change(amount);    return 0;}
0 0
原创粉丝点击