C语言去掉一个最高分一个最低分求最终成绩
时间: 2023-05-16 22:06:03 浏览: 169
假设有n个成绩,我们可以先将它们从小到大排序,然后去掉最低分和最高分,再将剩下的n-2个成绩求平均值即可得到最终成绩。具体的C语言代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int main() {
int n;
printf("请输入成绩个数:");
scanf("%d", &n);
int *scores = (int *)malloc(n * sizeof(int));
printf("请输入%d个成绩:", n);
for (int i = 0; i < n; i++) {
scanf("%d", &scores[i]);
}
qsort(scores, n, sizeof(int), cmp);
int sum = 0;
for (int i = 1; i < n - 1; i++) {
sum += scores[i];
}
printf("最终成绩为:%d\n", sum / (n - 2));
free(scores);
return 0;
}
```
相关问题
c语言使用函数去掉一个最高分和最低分求平均分
可以使用以下的 C 语言代码实现该功能:
```c
#include <stdio.h>
float avgWithoutMinMax(float arr[], int n) {
float sum = 0.0;
float min = arr[0], max = arr[0];
// 找出最大值和最小值
for (int i = 0; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
sum += arr[i];
}
// 减去最大值和最小值
sum = sum - min - max;
// 计算平均值
return sum / (n - 2);
}
int main() {
float scores[] = { 80.5, 90.0, 75.5, 85.0, 95.0 };
int n = sizeof(scores) / sizeof(scores[0]);
float avg = avgWithoutMinMax(scores, n);
printf("平均分是: %.2f", avg);
return 0;
}
```
该程序使用了一个 `avgWithoutMinMax` 函数来计算去掉最高分和最低分后的平均值。首先,它遍历一遍数组来找出最大值和最小值,并累加所有分数的和。然后,它减去最大值和最小值,最后计算平均值并返回。在 `main` 函数中,我们定义了一个数组 `scores` 来存储成绩,并使用 `sizeof` 函数来计算数组的长度。最后,我们调用 `avgWithoutMinMax` 函数来计算平均分,并使用 `printf` 函数输出结果。
用c语言写一个五个评委为参赛选手打分,选手最后得分要去掉一个最高分,去掉一个最低分,然后求平均分作为选手成绩。不用数组
您好!感谢您使用 ChitGPT 进行对话。根据您的需求,我可以提供以下 C 语言代码,实现对五个评委为参赛选手打分,选手最后得分要去掉一个最高分,去掉一个最低分,然后求平均分作为选手成绩的功能,但不使用数组:
```c
#include <stdio.h>
#include <limits.h>
int main() {
int judge1, judge2, judge3, judge4, judge5;
int highest, lowest, sum, avg;
sum = 0;
highest = INT_MIN; /* initialize as smallest value */
lowest = INT_MAX; /* initialize as largest value */
/* read in scores from the judges */
printf("Enter score from judge 1: ");
scanf("%d", &judge1);
printf("Enter score from judge 2: ");
scanf("%d", &judge2);
printf("Enter score from judge 3: ");
scanf("%d", &judge3);
printf("Enter score from judge 4: ");
scanf("%d", &judge4);
printf("Enter score from judge 5: ");
scanf("%d", &judge5);
/* find the highest and lowest scores */
if (judge1 > highest) {
highest = judge1;
}
if (judge1 < lowest) {
lowest = judge1;
}
if (judge2 > highest) {
highest = judge2;
}
if (judge2 < lowest) {
lowest = judge2;
}
if (judge3 > highest) {
highest = judge3;
}
if (judge3 < lowest) {
lowest = judge3;
}
if (judge4 > highest) {
highest = judge4;
}
if (judge4 < lowest) {
lowest = judge4;
}
if (judge5 > highest) {
highest = judge5;
}
if (judge5 < lowest) {
lowest = judge5;
}
/* calculate the sum of the scores */
sum = judge1 + judge2 + judge3 + judge4 + judge5 - highest - lowest;
/* calculate the average score */
avg = sum / 3;
/* print out the final score */
printf("Final score: %d\n", avg);
return 0;
}
```
希望这个代码能够满足您的需求。如果还有其他需要,请随时问我。
阅读全文