在计算BMI(BodyMassIndex ,身体质量指数)的案例基础上,判断人体胖瘦程度。BMI中国标准如下表所示。
多组输入,每一行包括两个整数,用空格隔开,分别为体重(公斤)和身高(厘米)。
针对每行输入,输出为一行,人体胖瘦程度,即分类。
80 170 60 170 90 160 50 185
Overweight Normal Obese Underweight
#include<bits/stdc++.h>
using namespace std;
int main() {
double a, b;
while(cin >> a >> b) {
b /= 100.0;
a /= b*b;
if(a<18.5)
cout << "Underweight" << endl;
else if(a<=23.9)
cout << "Normal" << endl;
else if(a<=27.9)
cout << "Overweight" << endl;
else
cout << "Obese" << endl;
}
return 0;
} #include<stdio.h>
#include<math.h>
int main()
{
int weight,height;
float BMI;
while(~scanf("%d%d",&weight,&height))
{
BMI=weight/pow(height/100.0,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 if(BMI>27.9)printf("Obese\n");
}
return 0;
}第一次超越100%
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>
#include <math.h>
int main() {
int weight = 0;
int hight = 0;
while (~scanf("%d %d", &weight, &hight)) {
double bmi = weight / pow(hight / 100.0,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 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 hight = 0;
while (scanf("%d %d", &weight, &hight) != EOF)
{
double BMI = weight / ((hight / 100.0) * (hight / 100.0));
if (BMI < 18.5)
{
printf("Underweight\n");
}
else if (BMI >= 18.5 && BMI <= 23.8)
{
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=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;
} import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* @Title: 衡量人体胖瘦程度
* @Remark: 在计算BMI(BodyMassIndex ,身体质量指数)的案例基础上,判断人体胖瘦程度。
* 输入描述:
* 多组输入,每一行包括两个整数,用空格隔开,分别为体重(公斤)和身高(厘米)。
* 输出描述:
* 针对每行输入,输出为一行,人体胖瘦程度,即分类。
*
* @Author: ijunfu
* @Version: 1.0.0
* @Date: 2022-03-19
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
List<Integer> list = Arrays.stream(in.nextLine().split(" ")).map(x -> Integer.valueOf(x)).collect(Collectors.toList());
int w = list.get(0);
int h = list.get(1);
double bmi = w / Math.pow(h/100.0, 2);
String str = "";
if(bmi <18.5) {
str = "Underweight";
} else if(18.5<= bmi && bmi <= 23.9) {
str = "Normal";
} else if(23.9< bmi && bmi <= 27.9) {
str = "Overweight";
} else {
str = "Obese";
}
System.out.println(str);
}
}
}
#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条件嵌套
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sin = new Scanner(System.in);
while(sin.hasNext()){
long w = sin.nextInt(); // 体重
double h = sin.nextInt(); // 身高 必须使用double 来接收不然计算精度或变化
double d = h / 100; // 把身高变成厘米
double bml = w / (d * d); // 计算
if(bml < 18.5){
System.out.println("Underweight");
}else if(bml >= 18.5 && bml <= 23.9){
System.out.println("Normal");
}else if(bml >= 23.9 && bml <= 27.9){
System.out.println("Overweight");
}else if(bml > 27.9){
System.out.println("Obese");
}
}
}
} function getResult(str) {
let arr = str.split(' ');
let w = parseInt(arr[0]);
let h = parseInt(arr[1]) / 100;
let BMI = w / (h * h);
if (BMI < 18.5) {
return 'Underweight';
} else if (BMI >= 18.5 && BMI <= 23.9) {
return 'Normal';
} else if (BMI <= 27.9 && BMI > 23.9) {
return 'Overweight';
} else {
return 'Obese';
}
}
let line;
let str = '';
while (line = readline()) {
str += getResult(line) + '\n';
}
console.log(str);