您的位置:首页 > 其它

7-27 兔子繁衍问题-PTA(菜菜的方法)

2020-03-27 20:05 169 查看

只列前七个月很难总结出规律来,因为第三个月的兔子对数变化规律还不明朗,直到列出了第八个月和第九个月,才能发现,自第三个月起:

  • 本月成熟= 上月成熟 + 上月两个月龄兔;
  • 本月一月龄兔 = 本月成熟;
  • 本月二月龄兔 = 上月成熟;

刚刚看了别的答案,才发现原来,每月的兔子数 = 前两个月兔子数相加(吐血)

以下代码可直接运行参考:

#include<stdio.h>

/*  a+b+c=population
1   1+0+0
2   1+0+0
3   1+1+0
4   1+1+1
5   2+2+1
6   3+3+2
7   5+5+3
8   8+8+5
9   13+13+8
10  21+21+13*/

int main(){

int N,month = 0,population = 0,one_month,two_month,mature = 1,\
last_two_mon = 0,bottle;

scanf("%d",&N);
printf("\t\tmature\t\tone_month\ttwo_month\n");//打印出三个月以后兔子的数量变化
printf("\t\t成熟兔子\t一月龄兔子\t二月龄兔子\n");//打印出三个月以后兔子的数量变化
while(population < N){

++month;
if(month >3){
bottle = mature;
mature = last_two_mon + mature;
one_month = mature;
two_month = bottle;
last_two_mon = two_month;
population = mature + one_month + two_month;
printf("%d月:\t\t%d\t\t%d\t\t%d\n",month,mature,one_month,two_month);//打印出三个月以后兔子的数量变化
}else if(month == 3){
population = 2;
}else{
population = 1;
}
}
printf("\n兔子达到%d对需要%d个月\n",N,month);

return 0;
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
Big_Truck_ 发布了14 篇原创文章 · 获赞 0 · 访问量 317 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: