ε=(´ο`*))) 一个笔试题,就这样了,不想优化了 能出答案就行
import java.util.Scanner;
/** * 需求:给出一个数组,排序后,取相邻两值差的最大值 */public class maxInput { public static void main(String[] args) {Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String[] val = in.nextLine().split(","); int[] x = new int[val.length]; for (int i = 0; i < val.length; i++) { x[i] = Integer.parseInt(val[i]); } System.out.println(maximumGap(x)); }}
public static int maximumGap(int[] nums) {
if (nums.length<3) { return -1; } //冒泡排序 intArray(nums); //差值放入数组 int[] arr = new int[nums.length-1]; for (int i = 1; i < nums.length; i++) { arr[i-1] = nums[i]-nums[i-1]; } intArray(arr); //返回最后一个值 return arr[arr.length-1]; } //int数组冒泡排序 public static void intArray(int[] nums) { for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums.length-1-i; j++) { if (nums[j]>nums[j+1]) { int temp = nums[j+1]; nums[j+1] = nums[j]; nums[j] = temp; } } } }}