HDUOJ Children’s Queue

Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8835    Accepted Submission(s): 2813


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
大数。。 公式 f(x)=f(x-1)+f(x-2)-F(x-4);
代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #define maxn 250
 4 #define len 1000
 5 using namespace std;
 6 int a[len+1][maxn+1]={{1},{2},{4},{7}};
 7 int main()
 8 {
 9     int i,j,n,s,c=0;
10     for(i=4;i<=len;i++)
11     {
12         for(c=j=0;j<=maxn;j++)
13         {
14           s=a[i-1][j]+a[i-2][j]+a[i-4][j]+c;
15           a[i][j]=s%10;
16           c=(s-a[i][j])/10;
17         }
18     }
19     while(cin>>n)
20     {
21      for(i=maxn;a[n-1][i]==0;i--);
22      for(j=i;j>=0;j--)
23      {
24          printf("%d",a[n-1][j]);
25      }
26      printf("\n");
27     }
28     return 0;
29 }
View Code

 

posted @ 2013-08-04 15:05  龚细军  阅读(255)  评论(0编辑  收藏  举报