鱼C论坛

 找回密码
 立即注册
查看: 1815|回复: 4

[已解决]关于递归的一个小问题

[复制链接]
发表于 2022-5-5 15:32:29 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>

  2. void mypow(long long int a,int idx)
  3. {
  4.     if (a<10){
  5.         printf("%d %d \n",a%10,idx);
  6.      }
  7.     else
  8.     {
  9.         mypow(a/10,++idx);
  10.         printf("%d %d \n",a%10,idx);
  11.     }
  12.   
  13. }

  14. int main()
  15. {
  16.     long long int s=123456789;
  17.     mypow(s,100);
  18.     return 0;
  19. }

  20. /*
  21. 运行结果如下:
  22. PS D:\wp> ./ct
  23. 1 0
  24. 2 0
  25. 3 0
  26. 4 0
  27. 5 0
  28. 6 0
  29. 7 0
  30. 8 0
  31. 9 0
  32. 期望这个idx 是一个自增序列,却是获取了一堆000000
  33. */
复制代码
最佳答案
2022-5-5 16:18:46
  1. $ cat main.c
  2. #include <stdio.h>

  3. void mypow(long long int a,int idx)
  4. {
  5.     if (a<10){
  6.         //printf("%d %d \n",a%10,idx);
  7.         printf("%lld %d \n",a%10,idx);
  8.      }
  9.     else
  10.     {
  11.         mypow(a/10,++idx);
  12.         //printf("%d %d \n",a%10,idx);
  13.         printf("%lld %d \n",a%10,idx);
  14.     }
  15.   
  16. }

  17. int main()
  18. {
  19.     long long int s=123456789;
  20.     mypow(s,100);
  21.     return 0;
  22. }
  23. $ gcc-debug -o main main.c
  24. $ ./main
  25. 1 108
  26. 2 108
  27. 3 107
  28. 4 106
  29. 5 105
  30. 6 104
  31. 7 103
  32. 8 102
  33. 9 101
  34. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-5 16:15:46 | 显示全部楼层
我的运行结果是这个
  1. 1 108
  2. 2 108
  3. 3 107
  4. 4 106
  5. 5 105
  6. 6 104
  7. 7 103
  8. 8 102
  9. 9 101
复制代码

评分

参与人数 1鱼币 +5 收起 理由
wp231957 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-5 16:18:46 | 显示全部楼层    本楼为最佳答案   
  1. $ cat main.c
  2. #include <stdio.h>

  3. void mypow(long long int a,int idx)
  4. {
  5.     if (a<10){
  6.         //printf("%d %d \n",a%10,idx);
  7.         printf("%lld %d \n",a%10,idx);
  8.      }
  9.     else
  10.     {
  11.         mypow(a/10,++idx);
  12.         //printf("%d %d \n",a%10,idx);
  13.         printf("%lld %d \n",a%10,idx);
  14.     }
  15.   
  16. }

  17. int main()
  18. {
  19.     long long int s=123456789;
  20.     mypow(s,100);
  21.     return 0;
  22. }
  23. $ gcc-debug -o main main.c
  24. $ ./main
  25. 1 108
  26. 2 108
  27. 3 107
  28. 4 106
  29. 5 105
  30. 6 104
  31. 7 103
  32. 8 102
  33. 9 101
  34. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-5 16:20:24 | 显示全部楼层
6.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-5 16:35:38 | 显示全部楼层

嗯,光考虑a模10 是一个int  没考虑a 本身不是int
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-13 17:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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