C语言实现游泳比赛选手前三最佳成绩(无指针、需用函数)
解决方案
核心逻辑明确:游泳比赛成绩越短排名越靠前,需先计算每位选手的个人最佳成绩,再从8个最佳成绩中选出前三名。以下是分步实现方案:
1. 补充必要头文件
因需要复制字符串,需引入<string.h>头文件。
2. 实现计算个人最佳成绩的函数
该函数接收所有成绩数据、输入次数,输出每个选手的最小成绩:
void calculateBestTimes(float times[][8], int nTimes, float bestPerSwimmer[8]) { for (int c = 0; c < 8; c++) { // 初始化当前选手的最佳成绩为第一次输入的时间 bestPerSwimmer[c] = times[0][c]; // 遍历该选手的所有成绩,找出最小值 for (int f = 1; f < nTimes; f++) { if (times[f][c] < bestPerSwimmer[c]) { bestPerSwimmer[c] = times[f][c]; } } } }
3. 实现选出前三名的函数
该函数接收每个选手的最佳成绩和姓名,输出前三名的成绩与对应姓名:
void getTopThree(float bestPerSwimmer[8], char names[][50], float bestT[3], char bestN[3][50]) { // 初始化前三名成绩为极大值,确保任何有效成绩都能替换它 for (int i = 0; i < 3; i++) { bestT[i] = 10000.0f; bestN[i][0] = '\0'; // 初始化姓名为空字符串 } for (int c = 0; c < 8; c++) { // 逐个比较当前选手的最佳成绩与前三名 if (bestPerSwimmer[c] < bestT[0]) { // 优于第一名,将原名次依次后移 strcpy(bestN[2], bestN[1]); bestT[2] = bestT[1]; strcpy(bestN[1], bestN[0]); bestT[1] = bestT[0]; // 替换第一名 strcpy(bestN[0], names[c]); bestT[0] = bestPerSwimmer[c]; } else if (bestPerSwimmer[c] < bestT[1]) { // 优于第二名,将原第二名后移 strcpy(bestN[2], bestN[1]); bestT[2] = bestT[1]; // 替换第二名 strcpy(bestN[1], names[c]); bestT[1] = bestPerSwimmer[c]; } else if (bestPerSwimmer[c] < bestT[2]) { // 优于第三名,直接替换 strcpy(bestN[2], names[c]); bestT[2] = bestPerSwimmer[c]; } } }
4. 修改main函数逻辑
- 修复
scanf("%s", &names[c]);的错误,改为scanf("%s", names[c]);(数组名本身就是地址) - 声明存储每个选手最佳成绩的数组
float bestPerSwimmer[8]; - 调用上述两个函数完成计算
- 最后输出前三名结果
完整代码
#include <stdio.h> #include <string.h> void calculateBestTimes(float times[][8], int nTimes, float bestPerSwimmer[8]) { for (int c = 0; c < 8; c++) { bestPerSwimmer[c] = times[0][c]; for (int f = 1; f < nTimes; f++) { if (times[f][c] < bestPerSwimmer[c]) { bestPerSwimmer[c] = times[f][c]; } } } } void getTopThree(float bestPerSwimmer[8], char names[][50], float bestT[3], char bestN[3][50]) { for (int i = 0; i < 3; i++) { bestT[i] = 10000.0f; bestN[i][0] = '\0'; } for (int c = 0; c < 8; c++) { if (bestPerSwimmer[c] < bestT[0]) { strcpy(bestN[2], bestN[1]); bestT[2] = bestT[1]; strcpy(bestN[1], bestN[0]); bestT[1] = bestT[0]; strcpy(bestN[0], names[c]); bestT[0] = bestPerSwimmer[c]; } else if (bestPerSwimmer[c] < bestT[1]) { strcpy(bestN[2], bestN[1]); bestT[2] = bestT[1]; strcpy(bestN[1], names[c]); bestT[1] = bestPerSwimmer[c]; } else if (bestPerSwimmer[c] < bestT[2]) { strcpy(bestN[2], names[c]); bestT[2] = bestPerSwimmer[c]; } } } int main(){ int nTimes, f, c; float bestT[3]; char bestN[3][50]; char names[8][50]; float bestPerSwimmer[8]; // 用户输入成绩次数 printf("Input how many times you wish to input: "); scanf("%i", &nTimes); // 验证输入合法性 while(nTimes<1){ printf("\nMust be 1 or more: "); scanf("%i", &nTimes); } // 输入选手姓名 printf("\nInput each competitor's name: "); for(c=0; c<8; c++){ scanf("%s", names[c]); } // 存储所有成绩 float times[nTimes][8]; for(f=0;f<nTimes;f++){ for(c=0;c<8;c++){ printf("\nInput time %i for %s: ", f+1, names[c]); scanf("%f", ×[f][c]); while(times[f][c]<0){ printf("\nTime must be bigger than 0.00: "); scanf("%f", ×[f][c]); } } } // 计算每位选手的最佳成绩 calculateBestTimes(times, nTimes, bestPerSwimmer); // 选出前三名 getTopThree(bestPerSwimmer, names, bestT, bestN); // 输出结果 printf("\nTop 3 best times:\n"); for (int i = 0; i < 3; i++) { printf("%d. %s: %.2f\n", i+1, bestN[i], bestT[i]); } return 0; }
关键说明
- 所有逻辑拆分为独立函数,符合题目要求
- 未使用显式指针变量,仅用基础数组传参方式
- 保留了输入合法性验证逻辑
- 通过
strcpy完成字符串复制,确保姓名正确存储
内容的提问来源于stack exchange,提问作者Barto
相关产品推荐
相关产品推荐

