• HDOJ1297 Children’s Queue[递推+大数]


    Children’s Queue

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


    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
     
    Recommend
    lcy
     
     
     别人的解法:
     F(n)表示n个人的合法队列 
     按照最后一个人的性别分析,他要么是男,要么是女,所以可以分两大类讨论:
     
    1、如果n个人的合法队列的最后一个人是男,则对前面n-1个人的队列没有任何限制,他只要站在最后即可,所以,这种情况一共有F(n-1);
     
    2、如果n个人的合法队列的最后一个人是女,则要求队列的第n-1个人务必也是女生,这就是说,限定了最后两个人必须都是女生,这又可以分两种情况:
     
    2.1、如果队列的前n-2个人是合法的队列,则显然后面再加两个女生,也一定是合法的,这种情况有F(n-2);
     
    2.2、但是,难点在于,即使前面n-2个人不是合法的队列,加上两个女生也有可能是合法的,当然,这种长度为n-2的不合法队列,不合法的地方必须是尾巴,就是说,这里说的长度是n-2的不合法串的形式必须是“F(n-4)+男+女”,这种情况一共有F(n-4). 
     
    所以,通过以上的分析,可以得到递推的通项公式:
        F(n)=F(n-1)+F(n-2)+F(n-4) (n>3)
     
     
    知道递推公式就不难了- -
    code:
     1 import java.util.*;
     2  import java.math.*;
     3  
     4  public class Main
     5  {
     6      public static void main(String []args)
     7      {
     8          int n;
     9          int i;
    10          Scanner cin=new Scanner(System.in);
    11          BigInteger []result=new BigInteger[1002];
    12          result[1]=BigInteger.valueOf(1);
    13          result[2]=BigInteger.valueOf(2);
    14          result[3]=BigInteger.valueOf(4);
    15          result[4]=BigInteger.valueOf(7);
    16          for(i=5;i<=1000;i++)
    17              result[i]=result[i-1].add(result[i-2].add(result[i-4]));
    18          while(cin.hasNext())
    19          {
    20              n=cin.nextInt();
    21              System.out.println(result[n]);
    22          }
    23      }
    24  }






                If you have any questions about this article, welcome to leave a message on the message board.



    Brad(Bowen) Xu
    E-Mail : maxxbw1992@gmail.com


  • 相关阅读:
    2022.05.31软件更新公告
    为什么不网购方便面
    js localStorage
    uniapp上拉加载更多功能的简单实现
    Centos 系统 软raid 0 实现
    使用vscode调试ros
    wget 命令用法
    是否应该提前还房贷
    mybatis generator 自动生成的拼装sql分析
    动态样式+模板字符串
  • 原文地址:https://www.cnblogs.com/XBWer/p/2598180.html
Copyright © 2020-2023  润新知