题目描述
给出 n 和 n 个整数 ai,求这 n 个整数中的极差是什么。极差的意思是一组数中的最大值减去最小值的差。
输入格式
第一行输入一个正整数 n,表示整数个数。
第二行输入 n 个整数 a1,a2…an,以空格隔开。
输出格式
输出一个整数,表示这 n 个整数的极差。
输入输出样例
输入 #1
6 4 1 5 1 4 1
输出 #1
4
说明/提示
数据保证,1≤n≤100,0≤ai≤1000。
代码如下:
import java.util.Scanner;
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int max=-1,min=1001;
int a = scanner.nextInt();
for(int i=1;i<=a;i++){
int b=scanner.nextInt();
if(b>max) max=b;
if(b<min) min=b;
}
System.out.println(max-min);
}
}