c语言输入四个成绩求最高分最低分
时间: 2023-10-30 07:32:52 浏览: 92
你好,这是一个关于C语言的问题。输入四个成绩求最高分最低分的代码可以这样写:
#include <stdio.h>
int main() {
float score1, score2, score3, score4;
printf("请输入四个成绩:\n");
scanf("%f%f%f%f", &score1, &score2, &score3, &score4);
float max_score = score1;
if (score2 > max_score) {
max_score = score2;
}
if (score3 > max_score) {
max_score = score3;
}
if (score4 > max_score) {
max_score = score4;
}
float min_score = score1;
if (score2 < min_score) {
min_score = score2;
}
if (score3 < min_score) {
min_score = score3;
}
if (score4 < min_score) {
min_score = score4;
}
printf("最高分是:%.2f,最低分是:%.2f\n", max_score, min_score);
return 0;
}
希望能帮到您。如果您还有其他问题,可以继续问我。
相关问题
这是一个用c语言编写程序的问题,其要求输入某学生的四科成绩,然后求出总分和平均分,并找出最高分和最低分。
以下是用 C 语言编写的程序,可以实现输入某学生的四科成绩,然后求出总分和平均分,并找出最高分和最低分:
```c
#include <stdio.h>
int main()
{
float score1, score2, score3, score4, total, average;
float max_score, min_score;
printf("请输入四个科目的成绩:\n");
scanf("%f%f%f%f", &score1, &score2, &score3, &score4);
// 计算总分和平均分
total = score1 + score2 + score3 + score4;
average = total / 4;
// 找出最高分和最低分
max_score = score1;
if (score2 > max_score) {
max_score = score2;
}
if (score3 > max_score) {
max_score = score3;
}
if (score4 > max_score) {
max_score = score4;
}
min_score = score1;
if (score2 < min_score) {
min_score = score2;
}
if (score3 < min_score) {
min_score = score3;
}
if (score4 < min_score) {
min_score = score4;
}
printf("总分为:%.2f\n", total);
printf("平均分为:%.2f\n", average);
printf("最高分为:%.2f\n", max_score);
printf("最低分为:%.2f\n", min_score);
return 0;
}
```
程序运行后,会提示用户输入四个科目的成绩,用户输入完成后程序会计算总分和平均分,并找出最高分和最低分,最后输出结果。
C语言编程 输入某班C语言考试成绩,该班人数未知,用 -1 作为结束标志,若输入大于 100 分或小于 0 分,则重新输入,然后计算全班的最高分、最低分与平均分。
以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int score, count = 0, sum = 0, max = 0, min = 100;
// 循环输入成绩,直到输入 -1 结束
while (1) {
printf("请输入一个成绩:");
scanf("%d", &score);
// 判断是否结束
if (score == -1) {
break;
}
// 判断成绩是否合法
if (score < 0 || score > 100) {
printf("成绩不合法,请重新输入。\n");
continue;
}
// 统计信息
count++;
sum += score;
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
}
// 输出统计结果
if (count > 0) {
printf("最高分:%d\n", max);
printf("最低分:%d\n", min);
printf("平均分:%d\n", sum / count);
} else {
printf("没有输入成绩。\n");
}
return 0;
}
```
我们定义了四个变量:`score` 用于输入成绩,`count` 用于统计人数,`sum` 用于统计总分,`max` 和 `min` 分别用于记录最高分和最低分。
我们使用一个 `while` 循环来输入成绩,并在输入 `-1` 时结束循环。
在输入成绩时,我们先判断输入是否合法,若不合法则提示重新输入。
在统计信息时,我们对人数、总分、最高分和最低分进行了更新。
最后,我们输出统计结果。注意要先判断人数是否大于 0,否则输出没有输入成绩。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)