鱼C论坛

 找回密码
 立即注册
查看: 1806|回复: 2

[萌新报道] C语100题——11

[复制链接]
发表于 2020-6-29 13:00:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 LH魔王 于 2020-7-1 17:51 编辑
  1. //第十一题:
  2. //古典问题:有一对兔子,从出生后的第三个月起每个月都生一对兔子,小兔子长到第三个月
  3. //          后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

  4. #include<stdio.h>

  5. #if(0)
  6. int cal(int month);

  7. int main()
  8. {
  9.         int month,result;
  10.         printf("请输入您想知道的月份(直接输入一个整数就行):");
  11.         scanf("%d",&month);
  12.         result=cal(month);
  13.         printf("兔子的总数为:%d",result);
  14.         return 0;
  15. }

  16. int cal(int month)
  17. {
  18.         int result;
  19.         if (month==1||month==2)
  20.         {
  21.                 result=1;
  22.                 return result;
  23.         }
  24.         else
  25.         {
  26.                 result=cal(month-1)+cal(month-2);
  27.                 return result;
  28.         }
  29. }
  30. #endif

  31. int main()
  32. {
  33.         int month,result;
  34.         int m1=1,m2=1;
  35.         printf("请输入您想知道的月份(直接输入一个整数就行):");
  36.         scanf("%d",&month);
  37.         if(month==1||month==2)
  38.         {
  39.                 result=1;
  40.         }
  41.        
  42.         while (month-2&&month-1)
  43.         {
  44.                 result=m1+m2;
  45.                 m1=m2;
  46.                 m2=result;
  47.                 month--;
  48.         }
  49.         printf("兔子的总数为:%d",result);
  50.         return 0;
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-29 13:01:28 | 显示全部楼层
里面是递归和迭代的,把宏定义改一下就行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-29 13:02:03 | 显示全部楼层
  1. //第十一题:
  2. //古典问题:有一对兔子,从出生后的第三个月起每个月都生一对兔子,小兔子长到第三个月
  3. //          后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

  4. #include<stdio.h>

  5. #if(0)
  6. int cal(int month);

  7. int main()
  8. {
  9.         int month,result;
  10.         printf("请输入您想知道的月份(直接输入一个整数就行):");
  11.         scanf("%d",&month);
  12.         result=cal(month);
  13.         printf("兔子的总数为:%d",result);
  14.         return 0;
  15. }

  16. int cal(int month)
  17. {
  18.         int result;
  19.         if (month==1||month==2)
  20.         {
  21.                 result=1;
  22.                 return result;
  23.         }
  24.         else
  25.         {
  26.                 result=cal(month-1)+cal(month-2);
  27.                 return result;
  28.         }
  29. }
  30. #endif

  31. #if(1)
  32. int main()
  33. {
  34.         int month,result;
  35.         int m1=1,m2=1;
  36.         printf("请输入您想知道的月份(直接输入一个整数就行):");
  37.         scanf("%d",&month);
  38.         if(month==1||month==2)
  39.         {
  40.                 result=1;
  41.         }
  42.        
  43.         while (month-2&&month-1)
  44.         {
  45.                 result=m1+m2;
  46.                 m1=m2;
  47.                 m2=result;
  48.                 month--;
  49.         }
  50.         printf("兔子的总数为:%d",result);
  51.         return 0;
  52. }
  53. #endif
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 06:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表