最少几道题的,你们的思路呢? public class JR2 { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = in.nextInt(); } System.out.println(solution(arr)); } public static int solution(int[] arr) { mergeSort(arr, 0, arr.length - 1); int count = 0; int flag = 1; for (int i = 0; i < arr.length - 1; i++) { if (Math.abs(arr[i] - arr[i + 1]) <= 10) { if (flag == 3) { flag = 1; } else { flag += 1; } } else { if (flag == 3) { flag = 1; } else { count += 3 - flag; flag = 1; } } } if (flag!=3){ count+=3-flag; } return count; } public static void merge(int[] arr, int low, int mid, int height) { int i = low; int j = mid + 1; int k = 0; int[] temp = new int[height - low + 1]; while (i <= mid && j <= height) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; } } while (i <= mid) { temp[k++] = arr[i++]; } while (j <= height) { temp[k++] = arr[j++]; } for (int m = 0; m < temp.length; m++) { arr[low + m] = temp[m]; } } public static void mergeSort(int[] arr, int low, int height) { if (low < height) { int mid = (low + height) / 2; mergeSort(arr, low, mid); mergeSort(arr, mid + 1, height); merge(arr, low, mid, height);   } }
点赞 评论

相关推荐

object3:开始给部分🌸孝子上人生第一课了
点赞 评论 收藏
分享
牛客网
牛客企业服务