小明最近喜欢玩一款新游戏。在该游戏中,需要组建队伍去完成任务以获取奖励。小明挑出了所有的队员(每个人能力不同),需要一个函数挑出队长(能力最强的队员)。 函数接口定义: 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 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB C (gcc) 1 测试用例 上一题 提交本题作答 下一题
时间: 2024-04-07 10:27:54 浏览: 117
下面是一份可行的 Python 代码实现:
```python
NAME_LEN = 100
SEX_LEN = 6
class TeamMember:
def __init__(self, id, lastname, firstname, sex, ability):
self.id = id
self.lastname = lastname
self.firstname = firstname
self.sex = sex
self.ability = ability
def showCaptain(team, n):
max_ability = 0.0
max_index = 0
for i in range(n):
if team[i].ability > max_ability:
max_index = i
max_ability = team[i].ability
print(team[max_index].id, team[max_index].lastname, team[max_index].firstname,
team[max_index].sex, "%.2f" % team[max_index].ability)
if __name__ == '__main__':
n = int(input())
team = []
for i in range(n):
line = input().split()
id = int(line[0])
lastname = line[1]
firstname = line[2]
sex = line[3]
ability = float(line[4])
member = TeamMember(id, lastname, firstname, sex, ability)
team.append(member)
showCaptain(team, n)
```
函数 `showCaptain` 的实现思路与 C 语言版本是一样的,遍历整个队伍,找到能力值最大的队员,然后输出其信息。不同之处在于 Python 中没有指针的概念,因此用一个类代替了 C 语言中的结构体。
在输出能力值时,需要使用 `"%.2f" % team[max_index].ability` 的方式将其保留两位小数。
阅读全文