Problem Description
There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM Here F stands for a girl and M stands for a boy. The total number of queue satisfied the headmaster’s needs is 7. Can you make a program to find the total number of queue with n children? |
Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of children (1<=n<=1000)
|
Output
For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.
|
Sample Input
1 2 3 |
Sample Output
1 2 4 |
Author
SmallBeer (CML)
|
Source
杭电ACM集训队训练赛(VIII)
|
Recommend
lcy
|
先写出前面几个来,可以发现规律f[i]=f[i-1]+f[i-2]+f[i-4];注意当1000的时候会爆long long所以要用大数!!!
代码:
#include<stdio.h>
#include<string.h>
char a[1005][1001];
void add(int x,int y){//计算两个数相加,将各个位置的数转化为int型进行计算,然后判断是否大于10,大于10则将z=1,否则赋为0.
int len_a,len_b;
len_a=strlen(a[x]);
len_b=strlen(a[y]);
int z=0;
int tx,ty,t,i=0;
while(i<len_a||z==1){//如果最后z=1则继续循环将1加到末尾
tx=ty=t=0;
if(i<len_a)
tx=a[x][i]-'0';//如果i>=len_a则tx为之前赋的0值,对计算无影响了。
if(i<len_b)
ty=a[y][i]-'0';//同上。
t=tx+ty+z;//计算两个数同一位数的值,如果上两个数的值大于10则加上z的值否则为0,对计算无影响
if(t>=10){//判断此时的值是否大于10,如果大于10则t-10&&z=1
t=t-10;
z=1;
}
else//如果此时t不大于10则赋0。
z=0;
a[x][i]=t+'0';
i++;
}
}
int main()
{
int n,i;
a[1][0]='1';
a[2][0]='2';
a[3][0]='4';
a[4][0]='7';
for(i=5;i<=1000;i++){
strcpy(a[i],a[i-1]);
add(i,i-2);//计算a[i-1]+a[i-2].
add(i,i-4);//计算a[i-1]+a[i-2]+a[i-4].
}
while(scanf("%d",&n)!=EOF){
for(i=strlen(a[n])-1;i>=0;i--)//注意将位置颠倒过来,i=0时是各位,输出时应是最高位。
printf("%c",a[n][i]);
printf("\n");
}
return 0;
}