/*
* 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.