检查并修改c语言程序:小明最近喜欢玩一款新游戏。在该游戏中,需要组建队伍去完成任务以获取奖励。小明挑出了所有的队员(每个人能力不同),需要一个函数挑出队长(能力最强的队员)。 函数接口定义: void showCaptain(TeamMember team[], int n); 参数说明:team中从下标0开始存放n个TeamMember,n>0。 函数功能:找出队长并输出其各项信息 裁判测试程序样例: #include<stdio.h> #include<stdlib.h> #define NAME_LEN 100 #define SEX_LEN 6 typedef struct { int id;//身份证号码 char lastname[NAME_LEN+1];//姓 char firstname[NAME_LEN+1];//名 char sex[SEX_LEN];//性别 double ability; } TeamMember; void showCaptain(TeamMember team[], int n); int main() { TeamMember *team; int n; int i; scanf("%d",&n); team = (TeamMember *)malloc(n*sizeof(TeamMember)); for(i=0;i<n;i++) { scanf("%d %s %s %s %lf",&team[i].id,team[i].lastname, team[i].firstname, team[i].sex, &team[i]. ability); } showCaptain(team, n); return 0; } /* 您提交的代码将放置在这里 */ 输入样例: 3 123456 zhang san male 100 123457 li si female 200.5 123458 wang ming male 159.1 输出样例: 123457 li si female 200.50。void showCaptain(TeamMember team[], int n) { int i,j; int max=team[0].ability; for(i=0;i<n;i++) { if(max<team[i].ability) max=team[i].ability; j=i; } printf("%d %s %s %s %.2lf",team[j].id,team[j].lastname,team[j].firstname,team[j].sex,team[j].ability); }
时间: 2024-03-31 15:35:34 浏览: 73
在函数 showCaptain 中,找最强的队员的逻辑有问题,应该在循环结束后再输出队长信息。修改后的代码如下:
void showCaptain(TeamMember team[], int n) {
int i;
int max_index = 0;
for (i = 1; i < n; i++) {
if (team[i].ability > team[max_index].ability) {
max_index = i;
}
}
printf("%d %s %s %s %.2lf", team[max_index].id, team[max_index].lastname, team[max_index].firstname, team[max_index].sex, team[max_index].ability);
}
阅读全文