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 }
  • 相关阅读:
    SVN版本库备份和恢复
    Jira的安装使用
    前端CSS的选择器整理搜集 s
    事务复制在不重新初始化的情况下添加新表
    没有共享存储和仲裁盘的SQL Server 2012HADR故障手动切换TSQL
    SQLSERVER聚集索引的整理(重建)的必要性测试
    sqlserver字符串与表格互相转换的函数和方法
    当一个DBA离职时,如何清除掉数据库里属于他的那些“东西”
    SQLSERVER复制订阅中的数据库版本选择
    1979年12月3日世界协调时间取代格林威治准时间
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2870947.html
Copyright © 2011-2022 走看看