题目描述:
给出n个正整数,要求找出相邻两个数字中差的绝对值最小的一对数字,如果有差的绝对值相同的,则输出最前面的一对数。
2<n<=100,正整数都在10^16范围内
输入包含2行,第一行为n,第二行是n个用空格分隔的正整数。
输出包含一行两个正整数,要求按照原来的顺序输出
样例输入
9
1 3 4 7 2 6 5 12 32
样例输出
3 4
代码:
import java.util.Scanner; public class test10 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] arr = new long[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextLong(); } long[] res = new long[]{arr[0],arr[1]}; long min = Math.abs(arr[1] -arr[0]); for(int i = 1; i < n; i++){ long temp = Math.abs(arr[i] - arr[i-1]); if( temp < min){ min = temp; res[0] = arr[i-1]; res[1] = arr[i]; } } System.out.println(res[0] + " " + res[1]); } }