杭电 2100 Children’s Queue

Children’s Queue

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

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(i - 1);
②如果是女生,而f(i - 1)的最后一个有可能是男生,直接加女生不成立。所以就在f(i - 2)的后面直接放两个女生就满足了条件。 可是 还有一种特殊情况 ~~(>_<)~~,如果f(i - 2)的最后只有一女生(这种情况在f(i - 2)中不存在,但是在f(i)中可能存在,即连续三个女生。)这个时候就应该加上f(i - 4)(即f(i - 4)后面放一个男生 + 一个女生 + 两个女生)。
所以递推公式为 :
f(n)= f(n - 1)+ f(n - 2)+ f(n - 4)
接下来 就是大数问题了 O(∩_∩)O

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[7500][800]={0};
int main()
{
    int i,j,k,l;
    a[1][0]=1;
    a[0][0]=1;
    a[2][0]=2;
    a[3][0]=4;
    k=0;
    for(i=4;i<7500;i++)
    {
        for(j=0;j<=k;j++)
        {
            a[i][j]+=a[i-1][j]+a[i-2][j]+a[i-3][j];
            if(a[i][j]>=10)
            {
                a[i][j+1]=a[i][j]/10;
                a[i][j]%=10;
            }
        }
        if(a[i][j]>=10)
        {
            k++;
        }
    }
    while(scanf("%d",&l)!=EOF)
    {
        for(i=k;i>=0;i--)
            if(a[l][i]!=0)
                break;
        for(;i>=0;i--)
            printf("%d",a[l][i]);
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nanfenggu/p/7900187.html