[sicily online]1029. Rabbit

/*
1,大整数加法,用string模拟
2,用一个数组模拟兔子成长过程
Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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
*/
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<list>
#include<map>
#include<string.h>
using namespace std;

string strPlus(string x1,string x2)
{
	for(string::size_type i=0;i<x2.size();i++)
	{
		if(i>=x1.size())
			x1.push_back('0');
		if(i>=x2.size())
			break;
		int num1=x1[i]-'0';
		int num2=x2[i]-'0';
		int sum=num1+num2;
		x1[i]=sum%10+'0';
		if(sum<10)
			continue;
		int j=i+1;
		while(1)
		{
			if(j>=x1.size())
			{
				x1.push_back('1');
				break;
			}
			if(x1[j]=='9')
			{
				x1[j]='0';
				j++;
			}
			else
			{
				x1[j]++;
				break;
			}
		}
	}
	return x1;
}

int main()
{
	int m,d;
	while(cin>>m>>d&&m!=0)
	{
		vector<string> mouths(m);
		mouths[m-1]="1";
		while(d--)
		{
			string tmpLast=mouths[m-1];
			if(m>1)
				mouths[m-1]=strPlus(mouths[m-1],mouths[m-2]);
			else mouths[m-1]=strPlus(mouths[m-1],mouths[m-1]);
			for(int i=m-3;i>=0;i--)
			{
				mouths[i+1]=mouths[i];
			}//end for
			if(m>1)
				mouths[0]=tmpLast;
		}
		string sum="0";
		for(int i=0;i<m;i++)
			sum=strPlus(sum,mouths[i]);
		reverse(sum.begin(),sum.end());
		cout<<sum<<endl;
	}//end while
}

你可能感兴趣的:([sicily online]1029. Rabbit)