用 max 在一行 n 个成绩中直接取最高分。
OJ: noi_openjudge
题目 ID: ch0105-05
难度:入门
标签:循环python
日期: 2026-07-30 23:01
题意
读入
思路
题目保证至少有一名学生,因此可将 map(int, input().split()) 直接传入 max。迭代器逐个提供成绩,不需要额外保存列表。
代码
Python代码
python
count = int(input())
scores = map(int, input().split())
print(max(scores))C++代码
cpp
#include <cstdio>
int main(){
int n;
int sum = 0;
int i,t;
scanf("%d",&n);
for (i=1;i<=n;i++){
scanf("%d",&t);
if( sum < t)
sum = t;
}
printf("%d\n",sum);
return 0;
}复杂度
时间复杂度为
总结
求单个最值时,内建 max 比手写初值和循环更新更简洁。