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?
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
递推题:
当第n个为男生的时候,只需计算当有n-1个人时有多少种正确的排序既可。
当第n个为女生时,此时要分两种情况讨论:
1、第n-1个为女生,此时就只需计算当有n-2个人时有多少种正确的排序可能既可。
2、第n-1个为女生但是第n-2也为女生第n-3个为男生,因为第n、n-1个都为女生所以第n-2个并不是单独一个女生站列,此时不属于上叙的有n-2个人时的正确排序(从n-3个人开始的排序为FM。。女生单独排在最后面,不是正确的排序,所以单独考虑),所以此时只需计算当有n-4个人时有多少正确排序。
综上可以得出递推公式为a[n]=a[n-1]+a[n-2]+a[n-4]。
此题仅仅只得出递推公式进行计算还是Wrong Answer。因为n可以取1000的值此时a[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;
}
#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;
}