在计算BMI(BodyMassIndex ,身体质量指数)的案例基础上,判断人体胖瘦程度。BMI中国标准如下表所示。
多组输入,每一行包括两个整数,用空格隔开,分别为体重(公斤)和身高(厘米)。
针对每行输入,输出为一行,人体胖瘦程度,即分类。
80 170 60 170 90 160 50 185
Overweight Normal Obese Underweight
int main() {
float a, b;
while (scanf("%f %f", &a, &b) == 2) {
float bmi = a / ((b / 100) * (b / 100));
if (bmi < 18.5)printf("Underweight\n");
else if (bmi >= 18.5 && bmi <= 23.9)printf("Normal\n");
else if (bmi >= 23.9 && bmi <= 27.9)printf("Overweight\n");
else if (bmi >= 27.9)printf("Obese\n");
}
return 0;
} #include <stdio.h>
#include <math.h>
enum
{
underweight,
normal,
overweight,
obese
};
int main()
{
int height = 0;
int weight = 0;
float bmi = 0.0f;
int choose = 0;//用来做下标
char s[][15] = {"Underweight", "Normal", "Overweight", "Obese"};
while(scanf("%d%d", &weight, &height) != EOF)
{
//录入的身高是m,计算的身高是cm,记得换算
bmi = (float)weight / pow((height / 100.0), 2);
if(bmi < 18.5)
{
choose = underweight;
}
else if (bmi >= 18.5 && bmi <=23.9)
{
choose = normal;
}
else if (bmi > 23.9 && bmi <= 27.9)
{
choose = overweight;
}
//这里也可以直接写成 else,但为了更易理解,还是写了个必定执行的条件
else if(bmi > 27.9)
{
choose = obese;
}
printf("%s\n", s[choose]);
}
return 0;
} #include<stdio.h>
int main()
{
int a,b;
int BMI;
printf("输入体重和身高:\n");
scanf("%d%d",&a,&b);
BMI=a/((b/100)*(b/100));
if(BMI<18.5)
{
printf("undeweight\n");
}
if(BMI>=18.5&&BMI<=23.9)
{
printf("normal\n");
}
if(BMI>23.9&&BMI<=27.9)
{
printf("overweight\n");
}
else
{
printf("Obese\n");
}
return 0;
} #include <stdio.h>
int main() {
int weight = 0, high = 0;
float BMI = 0;
while (scanf("%d %d", &weight,&high) != EOF) {
BMI = weight / ((high / 100.0) * (high / 100.0));
if (BMI < 18.5) {
printf("%s\n", "Underweight");
} else if (BMI >= 18.5 && BMI <= 23.9) {
printf("%s\n", "Normal");
} else if (BMI > 23.9 && BMI <= 27.9) {
printf("%s\n", "Overweight");
} else if (BMI > 27.9) {
printf("%s\n", "Obese");
}
}
return 0;
} #include<stdio.h>
int main()
{
int weight=0;
int height=0;
double bmi=0;
while(scanf("%d %d",&weight,&height)!=EOF)
{
bmi=weight/((height/100.0)*(height/100.0));
if(bmi<18.5)
printf("Underweight");
else if(bmi>=18.5&&bmi<=23.9)
printf("Normal");
else if(bmi>23.9&&bmi<=27.9)
printf("Overweight");
else
printf("Obese");
printf("\n");
}
return 0;
} #include <stdio.h>
int main()
{
int wt=0;
int ht=0;
float BMI=0;
while(~scanf("%d%d",&wt,&ht))
{
BMI=wt/(ht*ht/100.0/100.0);
printf("%s\n",BMI<18.5?"Underweight":(BMI>=18.5&&BMI<=23.9)?"Normal":(BMI>27.9)?"Obese":"Overweight");
}
return 0;
} #include<stdio.h>
int main(void){
int kg = 0, cm = 0;
float bmi = 0.0;
while(scanf("%d %d", &kg, &cm) != EOF){
bmi = kg / (cm * cm / 10000.0);
if(bmi < 18.5)
printf("Underweight\n");
else if(bmi <= 23.9)
printf("Normal\n");
else if(bmi <= 27.9)
printf("Overweight\n");
else
printf("Obese\n");
}
return 0;
}
没啥难度,就不写注释了,主要还是练习一下,连续从键盘输入中获取值并赋值给变量,还有熟悉if-else if-else条件嵌套
#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d %d\n",&a,&b)!=EOF)
{
(float)a;\\为什么用强转后最后结果不对,全都是Obese啊\\
(float)b;
b=b/100.0;
float bmi;
bmi=a/(b*b);
if(bmi>27.9) printf("Obese\n");
else if(bmi<=27.9&&bmi>23.9) printf("Overweight\n");
else if(bmi<=23.9&&bmi>=18.5) printf("Normal\n");
else if(bmi<18.5) printf("Underweight\n");
}
return 0;
} #include<stdio.h>
#include<math.h>
int main()
{
int weight,height;
float bmi;
while(scanf("%d %d ",&weight,&height)!=EOF)
{
height = height * 0.01;
bmi = weight/pow((double)height,2);
//bmi = weight/(height/100.0*height/100.0);
//bmi = weight/pow((double)height*0.01,2);
if(bmi<18.5)
{
printf("Underweight\n");
}
else if(bmi>=18.5&&bmi<=23.9)
{
printf("Normal\n");
}
else if(bmi>23.9&&bmi<=27.9)
{
printf("Overweight\n");
}
else
{
printf("Obese\n");
}
}
return 0;
} 请教大佬为什么用注释中的代码就可以算出正确答案,而用上面的代码就无法算出正确的答案
#include<stdio.h>
int main()
{
int weight,height;
double bmi;
while(~scanf("%d %d",&weight,&height))
{
bmi=weight/((height/100.0)*(height/100.0));
if(bmi<18.5)
printf("Underweight\n");
else
if(bmi>=18.5&&bmi<=23.9)
printf("Normal\n");
else
if(bmi>23.9&&bmi<=27.9)
printf("Overweight\n");
else
printf("Obese\n");
}
return 0;
} #include <stdio.h>
int main()
#define b w/((h/100)*(h/100))
{
float w,h=0;
while(~scanf("%f %f",&w,&h))
{
if(b<18.5)
printf("Underweight\n");
else if(b>=18.5&&b<=23.9)
printf("Normal\n");
else if(b>27.9)
printf("Obese\n");
else
printf("Overweight\n");
}
return 0;
} #include<stdio.h>
void judge(int h,int w){
float b = 10000.0*h/(w*w);
if(b<18.5)
printf("Underweight\n");
else if(b>=18.5 && b<=23.9)
printf("Normal\n");
else if(b>23.9 && b<=27.9)
printf("Overweight\n");
else
printf("Obese\n");
}
int main()
{
int h,w;
while(~scanf("%d%d",&h,&w)){
judge(h,w);
}
return 0;
}