鱼C论坛

 找回密码
 立即注册
查看: 1275|回复: 6

[已解决]C语言

[复制链接]
发表于 2020-3-27 18:41:27 | 显示全部楼层 |阅读模式

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

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

x
题目要求:输出男人+女人+小孩的数量等于45的情况,用穷举法。



  1. #include<stdio.h>

  2. int main()
  3. {
  4.     int man,woman,child;

  5.     for(man = 0;man < 45;man+=3)
  6.     {
  7.         for(woman = 0; woman < 45; woman+=2)
  8.         {
  9.             for(child = 0; child < 45;child+=0.5)//这里的child+=0.5为什么不能正常输出,但是child+1就可以输出
  10.             {
  11.                 if(man + child + woman == 45)
  12.                 {
  13.                     printf("%dman+%dwoman+%dchild = 45\n",man,woman,child);
  14.                 }
  15.             }
  16.         }
  17.     }

  18. }

复制代码
最佳答案
2020-3-27 18:49:17
本帖最后由 良弓无箭 于 2020-3-27 18:53 编辑

child是int类型,所以不会正常输出。


  1. #include<stdio.h>

  2. int main()
  3. {
  4.     int man,woman;
  5.         float child;

  6.     for(man = 0;man < 45;man+=3)
  7.     {
  8.         for(woman = 0; woman < 45; woman+=2)
  9.         {
  10.             for(child = 0; child < 45;child+=0.5)//如果这里是child+=0.5,你下边需要输出%f
  11.             {
  12.                 if(man + child + woman == 45)
  13.                 {
  14.                     printf("%dman+%dwoman+%fchild = 45\n",man,woman,child);
  15.                 }
  16.             }
  17.         }
  18.     }

  19. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-27 18:45:00 | 显示全部楼层
你用int定义的child,给他加float类型的数据,
当然会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-27 18:47:47 | 显示全部楼层
qiuyouzhi 发表于 2020-3-27 18:45
你用int定义的child,给他加float类型的数据,
当然会报错

我用float试过了也不行哎
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 18:48:48 | 显示全部楼层
老牛来学习 发表于 2020-3-27 18:47
我用float试过了也不行哎

你给child赋了个整型值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 18:49:17 | 显示全部楼层    本楼为最佳答案   
本帖最后由 良弓无箭 于 2020-3-27 18:53 编辑

child是int类型,所以不会正常输出。


  1. #include<stdio.h>

  2. int main()
  3. {
  4.     int man,woman;
  5.         float child;

  6.     for(man = 0;man < 45;man+=3)
  7.     {
  8.         for(woman = 0; woman < 45; woman+=2)
  9.         {
  10.             for(child = 0; child < 45;child+=0.5)//如果这里是child+=0.5,你下边需要输出%f
  11.             {
  12.                 if(man + child + woman == 45)
  13.                 {
  14.                     printf("%dman+%dwoman+%fchild = 45\n",man,woman,child);
  15.                 }
  16.             }
  17.         }
  18.     }

  19. }

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

使用道具 举报

发表于 2020-3-27 20:39:57 | 显示全部楼层
输出也要用f
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-29 11:32:42 | 显示全部楼层
前边child改为float型后,输出也要变为%f输出。
float child;
printf(“%.1lfchild",child);
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 10:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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