在
C语言中,如果你想要计算一个演讲比赛中每个评委打分后得到的选手最终得分,通常我们会采用加权平均的方式来处理,尤其是如果评委的评分有一定的权重的话。但是,如果没有具体的权重分配,我们可以简单地取所有分数的算术平均值。
下面是一个基本的示例函数,它接收一个评委数量`n`和一个评委打分数组`scores`作为输入,并返回总的平均分:
```c
#include <stdio.h>
double calculateScore(int n, int scores[]) {
if (n <= 0 || n > 12) { // 确保评委数量合理
printf("Invalid number of judges.
");
return -1; // 返回错误值
}
int total = 0;
for (int i = 0; i < n; i++) {
total += scores[i]; // 计算总分
}
double average = (double)total / n; // 转换为浮点数并计算平均分
return average;
}
int main() {
int numJudges;
printf("Enter the number of judges (1-12): ");
scanf("%d", &numJudges);
int judgeScores[numJudges];
printf("Enter scores of each judge:
");
for (int i = 0; i < numJudges; i++) {
scanf("%d", &judgeScores[i]);
}
double finalScore = calculateScore(numJudges, judgeScores);
if (finalScore != -1) {
printf("The contestant
's final score is %.2f.