兔子繁衍问题—c语言

问题:
一对兔子,从出生后第三个月起每个月都生一对兔子。小兔子长到第三个月后每个月又生一对兔子。假如兔子都不死,请问第一个月出生的一对兔子,至少需要繁衍到第几个月时兔子总数才能到达n对?输入一个不超过10 000的正整数n,输出兔子总数达到n的最少月数,试编写相应程序。

思路

6492ea8d20df42059a2ee72eb753f096.png

编写程序

1.输入月份,输出兔子总数

#include<stdio.h>

int main()
{
    int month;
    scanf("%d",&month);
    int small = 1;
    int big = 1;
    int n = 3;
    int amount ;
    if(month == 1 || month == 2){
        printf("1\n");
    }else{
        while(n<=month){
            amount = small + big;
            int t = small;
            small = amount;
            big = t;
            n ++;
        }
        printf("第%d个月的兔子的个数是%d\n",month,amount);
    }
    
    return 0;
    
}

ff73b80af4c14f13806b2327ce87c45d.png 

2.输入兔子总数,输出月份

#include<stdio.h>
int main()
{
    int month = 2;
    //scanf("%d",&month);
    int small =1;
    int big = 1;
    int amount;
    int newamount;
    int m ;
    int a = 2;
    printf("请输入兔子数:\n");
    scanf("%d",&amount);
    if(amount == 1){
        printf("达到这样的兔子数是1月和2月\n");
    }else{
        while(newamount!=amount){
              a ++;
              newamount = big + small;
              m = small;
              small = newamount;
              big = m;
              
        }
        printf("达到%d只兔子需要%d个月\n",amount,a);
    }
    
    
    return 0;
    
  
}

04eeb986c6324b9a9ba1cfe359a2f2d3.png 

 

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值