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.
Sample input
2 3
3 5
1 100
0 0
Sample output
5
9
1267650600228229401496703205376
Code:
#include<iostream>
#include<cstring>
using namespace std;
const int NUM = 40;
const int ALL = 101;
char number[101][40];
void add(char *front,char * rear,char * result)
{
int count = 39;
int ahead = 0;
for(int i = 39; i >= 0; i-- )
{
int temp = front[count] -48 + rear[count] - 48 + ahead ;
if(temp >= 10)
{
ahead = 1;
result[count] = temp +38;
}
else
{
result[count] = temp + 48;
ahead = 0;
}
count --;
}
}
void rabbit(char front[][40],int m, int d)
{
for(int i = m; i < d+m; i++)
{
add(front[i-m],front[i-1],front[i]);
}
}
void show(char * front)
{
int recordfront = 0;
for(int i = 0; i < NUM; i++)
{
if(front[i] != '0')
{
recordfront = i;
break;
}
}
for(int i = recordfront; i < NUM ;i++)
{
cout << front[i];
}
cout << endl;
}
int main()
{
int m,d;
while(cin >> m >> d)
{
if(m == 0 && d == 0)
break;
else
{
for(int i = 0; i < ALL; i++)
{
for(int j = 0; j < NUM; j++)
{
number[i][j] = '0';
}
}
for(int i = 0; i < m; i++)
{
number[i][NUM-1] = '1';
}
rabbit(number,m,d);
show(number[d+m-1]);
}
}
return 0;
}

本文探讨了兔子繁殖问题,并提出了一种编程解决方案。通过调整斐波那契数列的参数,模拟不同环境下兔子数量的增长情况。输入包含多个测试用例,每个用例给出幼兔变为成年兔子所需月数及计算兔子总数的月份,输出为指定月份的兔子对数。
1026

被折叠的 条评论
为什么被折叠?



