直接取整数序列的最大值与最小值,并输出二者之差。
OJ: noi_openjudge
题目 ID: ch0109-05
难度:入门
标签:数组最值python
日期: 2026-07-30 23:01
题意
输出整数序列最大值减最小值。
思路
Python 的 max 和 min 可直接得到两个极值,再相减即可。
代码
Python代码
python
count = int(input())
numbers = list(map(int, input().split()))
print(max(numbers) - min(numbers))C++代码
cpp
#include <cstdio>
using namespace std;
#define inf 0x7f7f7f7f
int n;
int min = inf;
int max= -inf;
int main(){
int n;
int i,t;
scanf("%d",&n);
for (i=1;i<=n;i++){
scanf("%d",&t);
if( min > t)
min = t;
if( max < t)
max = t;
}
printf("%d",max-min);
return 0;
}复杂度
时间复杂度为
总结
极值差问题只依赖最大值和最小值。