C语言程序作业1(成绩的转化)(润年月份天数)(身高预测)(体型判断)
5.9 使用if-else语句编程,将输入的百分制成绩score转换成相应的五分制成绩后输出。
90<=score<=100 输出:A
80<=score<90 输出:B
70<=score<80 输出:C
60<=score<70 输出:D
0<=score<60 输出:E
如果score不在0~100之间,输出:Input error!
测试用例:95、85、75、65、55,分别输出A、B、C、D、E
#include<stdio.h> #include<stdlib.h> int main() { int score; scanf("%d",&score); if(90<=score&&score<=100) { printf("A\n"); } else if(80<=score&&score<90) { printf("B\n"); } else if(70<=score&&score<80) { printf("C\n"); } else if(60<=score&&score<70) { printf("D\n"); } else if(0<=score&&score<60) { printf("E\n"); } else { printf("Input error"); } return 0; }
5.10 从键盘输入某年某月(包括闰年),用switch语句编程输出该年的该月拥有的天数。要求考虑闰年以及输入月份不在合法范围内的情况。已知闰年的2月有29天,平年的2月有28天。
测试用例:
输入:2023 2 输出:28天
输入:2020 2 输出:29天
#include<stdio.h> #include<stdlib.h> int main() { int year,month; scanf("%d%d",&year,&month); switch (month) { case 1:case 3:case 5:case 7:case 8:case 10:case 12: printf("31天\n"); break; case 2: if(year%4==0&&year%100!=0||year%400==0) { printf("29天\n"); } else { printf("28天\n"); } break; case 4:case 6:case 9:case 11: printf("30天\n"); break; default:printf("Input error!\n"); } return 0; }
5.11 设faHeight为其父身高,moHeight为其母身高,身高预测公式为
男性成人时身高 = (faHeight + moHeight) * 0.54 cm
女性成人时身高 = (faHeight * 0.923 + moHeight) / 2.0 cm
此外,如果喜爱体育锻炼,那么可增加身高2%;如果有良好的卫生饮食习惯,那么可增加身高1.5%。
请编程从键盘输入用户的性别(用字符型变量sex存储,输入字符F表示女性,输入字符M表示男性)、父母身高(用实型变量存储,faHeight为其父身高,moHeight为其母身高)、是否喜爱体育锻炼(用字符型变量sports存储,输入字符Y表示喜爱,输入字符N表示不喜爱)、是否有良好的饮食习惯等条件(用字符型变量diet存储,输入字符Y表示良好,输入字符N表示不好),利用给定公式和身高预测方法对身高进行预测。
测试用例:
输入:M 180 160 Y Y 输出:190
输入: F 180 160 Y Y 输出:169
#include<stdio.h> int main(void) { char sex, sports, diet; int faHeight, moHeight; double myHeight; scanf("%c %d %d %c %c", &sex, &faHeight, &moHeight, &sports, &diet); if (sex == 'F') { myHeight = (faHeight * 0.923 + moHeight) / 2.0; if (sports == 'Y', diet == 'Y') myHeight = myHeight + (myHeight * 0.02) + (myHeight * 0.015); else if (sports == 'Y', diet == 'N') myHeight = myHeight + (myHeight * 0.02); else if (sports == 'N', diet == 'Y') myHeight = myHeight + (myHeight * 0.015); else (sports == 'N', diet == 'N'); myHeight =myHeight * 1; } else { myHeight = (faHeight + moHeight) * 0.54; if (sports == 'Y', diet == 'Y') myHeight = myHeight + (myHeight * 0.02) + (myHeight * 0.015); else if (sports == 'Y', diet == 'N') myHeight = myHeight + (myHeight * 0.02); else if (sports == 'N', diet == 'Y') myHeight = myHeight + (myHeight * 0.015); else(sports == 'N', diet == 'N'); myHeight = myHeight * 1; } printf("%.0lf\n", myHeight); return 0; }
#include<stdio.h> //#include<stdlib.h> int main() { char sex, sports, diet; float myHeight, faHeight, moHeight; //printf("Are you a boy(M) or a girl(F)? "); scanf(" %c",&sex); //printf("Please input your father's height(cm): ",&faHeight); scanf("%f",&faHeight); //printf("Please input your mother's height(cm): ",&moHeight); scanf("%f",&moHeight); //printf("Do you like sports(Y/N)? "); scanf(" %c",&sports); //printf("Do you have a good habit of diet(Y/N)? "); scanf(" %c",&diet); if(sex=='M' || sex=='m') myHeight = (faHeight + moHeight) * 0.54; else myHeight = (faHeight * 0.923 + moHeight) / 2.0; if(sports == 'Y' || sports == 'y') myHeight = myHeight * (1 + 0.02); if(diet == 'Y' || diet == 'y') myHeight = myHeight * (1 + 0.015); printf("%.0f\n",myHeight); //system("pause"); return 0; }
5.12 体型判断:
体指数 t = 体重 w /(身高 h)2 (w单位为千克,h单位为米)
当 t < 18 时,为低体重;
当 t 介于 18 和 25 之间时,为正常体重;
当 t 介于 25 和 27 之间时,为超重体重;
当 t >=27 时,为肥胖。
请使用 if语句 或 else- if语句编程,从键盘输入你的身高h 和 体重w,根据上述给定的公式计算体指数t,然后判断你的体重属于何种类型。
测试每一个分支,分别输出低体重、正常体重、超重体重、肥胖
#include <stdio.h> int main() { double t,h,w; scanf("%lf %lf",&h,&w); t=w/(h*h); if(t<18) printf("低体重\n"); else if (t>=18 && t<25) printf("正常体重\n"); else if (t>=25 && t<27) printf("超重体重\n"); else printf("肥胖\n"); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人