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 }

 

转载于:https://www.cnblogs.com/joyeecheung/archive/2013/01/22/2870947.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值