数学递推思想在程序中的应用(一)----------兔子问题

                                                Rabbit
  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
0 0

Sample output:
5
9
解题分析:

设函数fn(k)表示k(k<=d)个月后兔子对数,fn(0)=1,m表示变为成年兔子需要的月

数,经过d个月后计算兔子对数。
1.当m<d:
        1.1 若k-m<=0,fn(k)=fn(k-1)+1;
        1.2 若k-m>0,fn(k)=fn(k-1)+fn(k-m).
2.当m>=d,fn(k)=fn(k-1)+1;
程序代码(一):

#include<iostream>
#include<fstream>
using namespace std;
int growth_months=0,passed_months=0;
int rabbit_sum(int m,int p)
{
    if(growth_months<passed_months)
 {
  if(p==0){ return 1;}
  else if((p-m)<=0)
  {
      return rabbit_sum(m,p-1)+1;
  }
  else
  {
      return rabbit_sum(m,p-1)+rabbit_sum(m,p-m);
  }
 }
 else
 {  
  if(p==0){ return 1;}
     else{ return rabbit_sum(m,p-1)+1; }
 }
}
void main()
{
    ifstream stream("input.txt");
 while(!stream.eof())
 {
  stream>>growth_months>>passed_months;
  if(growth_months!=0)
  {  
   int getsum=rabbit_sum(growth_months,passed_months);
            cout<<getsum<<endl;
  }
  else {   break;   }
 }
}

程序代码(一)中当getsum的值超出int数据类型所表示的范围时就会发生输出错误。

下面的程序代码(二)可以消除这种隐患。

程序代码(二):

#include<stdio.h>
#include <string.h>
#include <fstream.h>

int rabbit[110][110];  //定义n个月兔子数,第一维表示n个月,第二维是高精度加法
int m,d; //兔子成熟期为m个月,求d个月之后兔子对总数量

void add(int a[], int b[], int c[])  //高精度加法 c=a+b,数组的0位表示最低位
{
 int i;
 memset (c, 0, sizeof(rabbit[0]));
 for (i=0; i<=105; i++)
 {
  int temp = a[i]+b[i]+c[i];   //计算第i位的和
  c[i] = temp%10;              //处理进位
  c[i+1] = temp/10;
 }
}

int main()
{
 ifstream filein("input.txt");   //读入数据
 filein >> m >> d;
 while (m!=0)
 {
  int i,j;
  memset(rabbit, 0, sizeof(rabbit));    //初始化
  rabbit[0][0] = 1;     //第一月为1对兔子

  for (i=1; i<=m; i++)
   rabbit[i][0] = i+1;     //对于前m个月,兔子增长数为1对

  for (i=m; i<=d; i++)
   add(rabbit[i-1], rabbit[i-m], rabbit[i]);  //计算前d个月的兔子对总数

  for (i=109; i>=0; i--)   //寻找最高位
  if (rabbit[d][i] != 0)
  {
   j = i;
   break;
  }
  for (; j>=0; j--)      //输出前d个月的兔子对总数
   printf("%d", rabbit[d][j]);
  printf("/n");
  filein >> m >> d;
 }
 filein.close();
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值