zoukankan      html  css  js  c++  java
  • 杭电 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;
    }
    
  • 相关阅读:
    从汇编看c++对静态成员的存取
    从汇编看c++内联函数评估求值
    从汇编看c++初始化列表初始化成员变量
    53. sql2005“备份集中的数据库备份与现有的xx数据库不同”解决方法
    52. 查看linux系统是32位还是64位
    51. linux卸载jdk
    50. linux下查看tomcat日志
    49. jdk-6u45-linux-i586.bin安装步骤
    48. Linux 删除文件夹命令
    47. linux下给已经存在的用户设置用户组
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7900187.html
Copyright © 2011-2022 走看看