鱼C论坛

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

[已解决]C语言

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

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

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

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

int main()
{
    int man,woman,child;

    for(man = 0;man < 45;man+=3)
    {
        for(woman = 0; woman < 45; woman+=2)
        {
            for(child = 0; child < 45;child+=0.5)//这里的child+=0.5为什么不能正常输出,但是child+1就可以输出
            {
                if(man + child + woman == 45)
                {
                    printf("%dman+%dwoman+%dchild = 45\n",man,woman,child);
                }
            }
        }
    }

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

child是int类型,所以不会正常输出。
#include<stdio.h>

int main()
{
    int man,woman;
        float child;

    for(man = 0;man < 45;man+=3)
    {
        for(woman = 0; woman < 45; woman+=2)
        {
            for(child = 0; child < 45;child+=0.5)//如果这里是child+=0.5,你下边需要输出%f 
            {
                if(man + child + woman == 45)
                {
                    printf("%dman+%dwoman+%fchild = 45\n",man,woman,child);
                }
            }
        }
    }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-27 18:45:00 | 显示全部楼层
你用int定义的child,给他加float类型的数据,
当然会报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

我用float试过了也不行哎
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

你给child赋了个整型值
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

child是int类型,所以不会正常输出。
#include<stdio.h>

int main()
{
    int man,woman;
        float child;

    for(man = 0;man < 45;man+=3)
    {
        for(woman = 0; woman < 45; woman+=2)
        {
            for(child = 0; child < 45;child+=0.5)//如果这里是child+=0.5,你下边需要输出%f 
            {
                if(man + child + woman == 45)
                {
                    printf("%dman+%dwoman+%fchild = 45\n",man,woman,child);
                }
            }
        }
    }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 20:39:57 | 显示全部楼层
输出也要用f
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-29 11:32:42 | 显示全部楼层
前边child改为float型后,输出也要变为%f输出。
float child;
printf(“%.1lfchild",child);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-11 02:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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