Square Coins
There are four combinations of coins to pay ten credits:
ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin.
Your mission is to count the number of ways to pay a given amount using coins of Silverland.
2 10 30 0
1 4 27
AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int c1[400],c2[400],i,j,k,n;
while(scanf("%d",&n)&&(n!=0))
{
for(i=0;i<=n;i++) //给第一个表达式初始化
{
c1[i]=1;
c2[i]=0;
}
for(i=2;i*i<=n;i++) //因为硬币都是数的平方,所以判断条件是i*i<=n
{
for(j=0;j<=n;j++)
{
for(k=0;k+j<=n;k=k+i*i) //注意k值的变化,k=k+i*i;因为硬币只有平方数,所以每次都要加平方数
{
c2[k+j]=c2[k+j]+c1[j];
}
}
for(j=0;j<=n;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
printf("%d\n",c1[n]);
}
return 0;
}
解题体会:母函数还是不理解,刚开始知道赋值的时候要修改但是还是不知道该修改哪里、改成什么