【深基4.习5】求极差 / 最大跨度值
题目描述
给出n(n≤100) 和 nn 个整数 ai(0≤ai≤1000),求这 n 个整数中的极差是什么。极差的意思是一组数中的最大值减去最小值的差。
输入格式
无
输出格式
无
输入输出样例
输入 #1复制
6
1 1 4 5 1 4
输出 #1复制
4
找最大最小减去就行,输入的时候顺便就找出来
AC代码(C++)
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
//特殊情况处理
if (n == 0)
cout << 0 << endl;
else if (n == 1)
{
int t;
cin >> t;
cout << t << endl;
}
else
{
//数值范围是0~1000所以下面的定义初值完全没问题
int max = -1;
int min = 1001;
int temp;
for (int i = 0; i < n; i++)
{
//一直寻找最大最小
cin >> temp;
if (temp > max)
max = temp;
if (temp < min)
min = temp;
}
cout << max - min << endl;
}
return 0;
}