Description
In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles?
Here is a sample tiling of a 2x17 rectangle.
Input
Input is a sequence of lines, each line containing an integer number 0 <= n <= 250.
Output
For each line of input, output one integer number in a separate line giving the number of possible tilings of a 2xn rectangle.
Sample Input
2
8
12
100
200
Sample Output
3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251
代码(单纯用数组做的,之前用的字符加数组,答案正确,不知为何WA):
单纯数组(AC):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int t[600],a[300][500];
int i,j;
memset(a,0,sizeof(a));
memset(t,0,sizeof(t));
a[0][0]=1; //坑爹的0的时候竟然是1!!!百思不得其解,但这样过了。
a[1][0]=1;
for(i=2;i<=250;i++)
{
for(j=0;j<=250;j++)
t[j]=a[i-1][j]+a[i-2][j]*2; //t[]是用来承接的,用完清空再用。
for(j=0;j<=250;j++)
{
t[j+1]+=(t[j]/10);
t[j]%=10;
}
for(j=0;j<=250;j++)
a[i][j]=t[j];
memset(t,0,sizeof(t));
}
int n;
while(scanf("%d",&n)!=EOF)
{
for(j=251;a[n][j]==0;j--);
for(;j>=0;j--)
printf("%d",a[n][j]);
printf("\n");
}
return 0;
}
代码2(字符加数组,和走楼梯思路一样,WA):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char ge[300][600]={"1","1","3"};
int a[600],b[600],i,j;
for(i=3;i<=250;i++)
{
int l1=strlen(ge[i-2]);
int l2=strlen(ge[i-1]);
for(j=0;j<l1;j++)
a[j]=ge[i-2][l1-j-1]-'0';
for(j=0;j<l2;j++)
b[j]=ge[i-1][l2-j-1]-'0';
int y=0;
for(j=0;j<l1;j++)
{
a[j]=a[j]*2+y;
y=0;
if(a[j]>9)
{a[j]=a[j]-10;
y=1;
}
}
l1++;
int k;
if(l1>=l2) k=l1;
else k=l2;
for(j=0;j<k;j++)
{
a[j]=a[j]+b[j];
if(a[j]>9)
{
a[j]=a[j]-10;
a[j+1]++;
}
}
for(j=500;a[j]==0;j--);
int x=j,x1=j;
for(j=0;j<=x;j++)
ge[i][j]=a[x1--]+'0';
ge[i][j]=0;
}
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%s\n",ge[n]);
}
return 0;
}