zoukankan      html  css  js  c++  java
  • sicily 1029. Rabbit

    Description

    The rabbits have powerful reproduction ability. One pair of adult rabbits can give birth to one pair of kid rabbits every month. And after m months, the kid rabbits can become adult rabbits.
        As we all know, when m=2, the sequence of the number of pairs of rabbits in each month is called Fibonacci sequence. But when m<>2, the problem seems not so simple. You job is to calculate after d months, how many pairs of the rabbits are there if there is exactly one pair of adult rabbits initially. You may assume that none of the rabbits dies in this period.

    Input

    The input may have multiple test cases. In each test case, there is one line having two integers m(1<=m<=10), d(1<=d<=100), m is the number of months after which kid rabbits can become adult rabbits, and d is the number of months after which you should calculate the number of pairs of rabbits. The input will be terminated by m=d=0.

    Output

    You must print the number of pairs of rabbits after d months, one integer per line.

    跟之前作业的那题有点不一样,用long long也不够,必须写成大精度加法

    一开始WA,发现大精度加法写错了,应该先加进位再取模而不是先取模再加,改掉之后AC,用时0.03s

    View Code
     1 #include<stdio.h>
     2 #define MAX 101
     3 int rabbit[MAX][MAX] = {1};
     4 void generate( int m, int d );
     5 void add( int des, int a, int b );
     6 
     7 int main()
     8 {
     9     int m, d;
    10     
    11     while( scanf("%d %d", &m, &d) && m != 0 )
    12     {
    13         generate(m, d);
    14     }
    15     return 0;
    16 }
    17 
    18 void add( int des, int a, int b )
    19 {
    20     int i, temp, carry = 0;
    21     
    22     for ( i = 0; i < MAX; i++ )
    23     {
    24         temp = rabbit[a][i] + rabbit[b][i] + carry;
    25         rabbit[des][i] = temp % 10;
    26         carry = temp / 10;
    27     }
    28     
    29 }
    30 
    31 void generate( int m, int d )
    32 {
    33     int i, j;
    34     
    35     for ( i = 1; i <= d; i++ )
    36         for ( j = 0; j < MAX; j++ )
    37             rabbit[i][j] = 0;
    38     
    39     for ( i = 1; i <= d; i++ )
    40     {
    41         if ( i < m )
    42             add( i, i - 1, 0 );
    43         else
    44             add( i, i - 1, i - m );
    45     }
    46     
    47     i = MAX - 1;
    48     while( rabbit[d][i] == 0)
    49         i--;
    50     
    51     for( j = i; j >= 0; j-- )
    52         printf("%d", rabbit[d][j]);
    53     
    54     printf("\n");
    55 }
  • 相关阅读:
    Python 中lambda 简单介绍
    关于python中带下划线的变量和函数 的意义
    Python 类的方法,下划线有什么不同?
    Python中用format函数格式化字符串的用法(2.7版本讲解哦!)
    如何理解 Python 中的__init__
    Python引用多个模块,调用模块中的函数时,要注意的地方
    Python tips: 什么是*args和**kwargs?
    如何简单地理解Python中的if __name__ == '__main__'
    NLP点滴——文本相似度
    马里奥AI实现方式探索 ——神经网络+增强学习
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2870947.html
Copyright © 2011-2022 走看看