问题:
很简单,先找面值大的。
#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;
}