第一题 ac
public class Alibishi1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int d = scanner.nextInt(); HashSet<Integer> set = new HashSet<>(); int[] arr = new int[n*n]; int max = Integer.MIN_VALUE; for (int i = 0; i < n*n ; i++) { int tmp = scanner.nextInt(); arr[i] = tmp; max = Math.max(max, tmp); set.add(tmp); } int cur = max; while(cur > 0){ if (set.contains(cur)){ set.remove(cur); } cur = cur - d; } if (set.size() > 0){ System.out.println(-1); return; } long sum = 0; for (int i = 0; i < n * n; i++) { sum += (max - arr[i]) / d; } System.out.println(sum); } }第二题 没来得及提交
public class Alibaba2 { public static int sum = 0; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char[] tmp = scanner.nextLine().toCharArray(); int n = Integer.parseInt(tmp[0] + ""); String[] strings = new String[n]; for (int i = 0; i < n; i++) { strings[i] = scanner.nextLine(); } for (int i = 0; i < n; i++) { sum = 0; System.out.println(method(strings[i])); } } public static int method(String s){ int[] arr = method2(s); way1(arr); way2(arr); return sum; } public static void way1(int[] arr){ int[] arrtmp1 = Arrays.copyOf(arr,arr.length); for (int i = 0; i < arr.length - 1; i++) { int p1 = 0; int p2 = 1; while(p2 < arr.length){ while(arrtmp1[p2] < 9 && arrtmp1[p1] > 0 && arrtmp1[p2] < 9){ arrtmp1[p1]--; arrtmp1[p2]++; sum++; } p1++; p2++; } } } public static void way2(int[] arr){ int[] arrtmp2 = Arrays.copyOf(arr,arr.length); for (int i = 0; i < arr.length - 1; i++) { int p1 = arr.length - 2; int p2 = arr.length - 1; while(p1 >= 0){ while(arrtmp2[p1] < 9 && arrtmp2[p1] < 9 && arrtmp2[p2] > 0){ arrtmp2[p1]++; arrtmp2[p2]--; sum++; } p1--; p2--; } } } public static int[] method2(String s){ char[] chars = s.toCharArray(); int[] nums = new int[chars.length]; for (int i = 0; i < nums.length; i++) { nums[i] = Integer.parseInt(chars[i]+""); } return nums; } }
全部评论
(1) 回帖