母函数请高手帮忙解释下程序。。
题目描述Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.
Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.
输入
The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
输出
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
样例输入
11
26
样例输出
4
13
下面是程序:
#include<iostream>
using namespace std;
int main()
{
int i,j,k,t;
int nums[251]={0},coins[5]={1,5,10,25,50};
int ans[251][101]={0},ansTemp[251][101]={0};
//…[i][j]表示i分钱由j个硬币组成的方案数
ans[0][0] = 1;
for(i=1;i<=5;i++)
{
for(j=0;j<=250;j++)//这个循环的作用?
for(k=0;k*coins[i-1]+j<=250;k++)//这里什么意思?。。
for(t=0;t+k<101;t++)//总的硬币数要少于100//这个也不明白
ansTemp[k*coins[i-1]+j][t+k] += ans[j][t];
for(j=0;j<=250;j++)
for(t=0;t<101;t++)
{
ans[j][t] = ansTemp[j][t];
ansTemp[j][t]=0;
}
}
for(i=1;i<=250;i++)
for(j=1;j<=100;j++)
nums[i] += ans[i][j];
nums[0] = 1;
while(cin>>i)
cout<<nums[i]<<endl;
return 0;
}
主要说明一下循环的意思循环思想越详细越好。。。。很看不明白。。。。。谢谢。。