/*
* Input:
n: the money you want to change;
unit: starts with the maximum change's unit, e.g. 50 (if there are changes 1, 5, 10, 25, 50);
Output:
ways: the number of ways of chaning the money n;
*/
#include <iostream>
#include <algorithm>
using namespace std;
int money_changes(int n, int unit)
{
int next_unit = 0;
switch(unit)
{
case 25:
next_unit = 10;
break;
case 10:
next_unit = 5;
break;
case 5:
next_unit = 1;
break;
case 1:
return 1;
}
int ways = 0;
for (int i = 0; i*unit <= n; i++)
{
ways += money_changes (n - i*unit, next_unit);
}
return ways;
}
int main()
{
cout << money_changes(100, 25) << endl;
return 0;
}
The problem of Money Changes
最新推荐文章于 2024-05-04 18:43:56 发布