[sicily online]1029. Rabbit

來源:互聯網
上載者:User
/*1,大整數加法,用string類比2,用一個數組類比兔子成長過程ConstraintsTime Limit: 1 secs, Memory Limit: 32 MBDescriptionThe 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.InputThe 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.OutputYou must print the number of pairs of rabbits after d months, one integer per line.Sample Input2 33 51 1000 0Sample Output591267650600228229401496703205376*/#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 forif(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}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.