一道有趣的编程题,不知道对不对
题目是我在别的论坛上找的,因为没有标准答案,不知道对不对?请高手们指出问题
/*题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的
兔子总数为多少?
这个题目相信大家都不会陌生了,那如果每个免子只能活10个月呢?
*/
# include <iostream.h>
//# include <windows.h>
void rabbit(int lifemonth,int month)
{
static int num=1; //设立静态变量,统计兔子个数
for(int x=0; x<lifemonth; x++) //循环10个月,看能生多少对
{
if(x>month) //判断是否超过月数
break;
if(x>2) //第3个月开始生
{
num+=2;
rabbit(lifemonth,month-x); //一只生一对,故调用2次
rabbit(lifemonth,month-x);
}
}
// system("cls");
cout << "after " << month << " month,"
<< "the total num of the rabbit is: " << num << endl;
}
void main()
{
int lifemonth=10; //兔子可以存活10个月
int month; //计算多少个月后兔子多少
cout << "please input the month" << endl;
cin >> month;
rabbit(lifemonth,month);
}