Question number: | 201712-1 |
Question name: | Minimum difference |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description Given n numbers, find the two numbers with the smallest difference (absolute value of the difference), and output the absolute value of their difference. Input format The first line of input contains an integer n . Output format Output an integer to indicate the answer. Sample input 5 Sample output 1 Sample description The two numbers with the smallest difference are 5 and 4, and the difference between them is 1. Sample input 5 Sample output 0 Sample description There are two identical numbers 3, and the difference between them is 0. Data size and convention For all evaluation cases, 2 ≤ n ≤ 1000, and each given integer is a positive integer not exceeding 10,000. |
import java.util.Scanner;
public class 最小差值 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int min=Math.abs(a[1]-a[0]);
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(Math.abs(a[i]-a[j])<min){
min=Math.abs(a[i]-a[j]);
}
}
}
System.out.println(min);
}
}