• HDOJ1297


    Children’s Queue

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


    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

    【大数的输出花费了好久时间。】

    /*
    题目大意:n个孩子站成一排,要求至少两个女孩站在一起。(男孩、女孩足够多),求有多少种站法。
    假设n-1个孩子已经站好了,现在第n个孩子过去。
    如果第n个孩子是男孩:则有f(n-1)种站法
    如果第n个孩子是女孩,那么第n-1个也必须是女孩:
    如果前n-2个孩子是合法的,那么就有f(n-2)种站法
    如果前n-2个孩子是不合法的,但是加上最后两个女孩就是合法的了,那么有f(n-4)种站法。
    n f
    0 1
    1 1
    2 2
    3 4
    4 7
    -----
    n可能为1000哦~所以要用大数。
    */
    #include <stdio.h>
    #include <string.h>
    #define N 100
    #define M 100000
    long a[1001][N];
    void Add(long *a,long *b,long *c)//c = a + b
    {
    int i;
    long carry;
    carry = 0L;
    for (i=N-1;i>=0;i--)
    {
    c[i]=a[i]+b[i]+carry;
    carry = 0L;
    if (c[i]>=M)
    {
    c[i]-=M;
    carry=1L;
    }
    }
    }
    void output(long *a)
    {
    int i=0;
    while(a[i] == 0L)
    i++;
    printf("%ld",a[i++]);//注意大数的输出格式。
    while (i!=N)
    {//注意a[i]小于M-1的位数的情况
    printf("%05ld",a[i++]);
    }
    printf("\n");

    }
    int main()
    {
    int n,i;
    long temp[N];
    memset(a,0L,sizeof(a));
    a[0][N-1]=1;
    a[1][N-1]=1;
    a[2][N-1]=2;
    a[3][N-1]=4;
    for (i=4;i<=1000;i++)
    {
    if (i==50)
    {
    i=50;
    }
    memset(temp,0L,sizeof(temp));
    Add(a[i-1],a[i-2],temp);
    Add(temp,a[i-4],a[i]);
    }
    while (scanf("%d",&n)!=EOF)
    {
    output(a[n]);
    }
    return 0;
    }
    字节跳动内推

    找我内推: 字节跳动各种岗位
    作者: ZH奶酪(张贺)
    邮箱: cheesezh@qq.com
    出处: http://www.cnblogs.com/CheeseZH/
    * 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    苹果推送APNS自己总结
    Windows下LDAP服务器配置
    service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误
    PHP 中的 9 个魔术方法
    使用 Equinox 开发 OSGi 应用程序
    使用 Equinox 框架进行 OSGi 环境下的 Web 开发
    探索 OSGi 框架的组件运行机制
    基于 OSGi 和 Spring 开发 Web 应用
    Equinox OSGI ServletBridge 原理与实践
    利用 Eclipse 开发基于 OSGi 的 Bundle 应用
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/2433422.html
Copyright © 2020-2023  润新知