美团9.13技术类编程题
五道题分别过了 91%, 100%, 45%, 55%, 100%
第一道, 二分,不知道漏了啥case,过了91%
public static void pro1(){ Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); int[][] arr = new int[n][m]; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ arr[i][j] = in.nextInt(); } } if(n % 2 == 1){ printArr(arr, n, m); return; } int row = n / 2; while(true){ for(int i = 0; i < row; i++){ for(int j = 0; j < m; j++) { if(arr[i][j] != arr[row + i][j]){ printArr(arr, row, m); return; } } } row = row / 2; } } private static void printArr(int[][] arr, int row, int m){ for(int p = 0; p < row; p++){ for(int q = 0; q < m; q++){ if(q == m - 1) System.out.println(arr[p][q]); else System.out.print(arr[p][q] + " "); } } }
第二道,简单的滑动窗口,AC
public static void prob2(){ Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = in.nextInt(); } int left = 0, right = 0; long total = 0; while(right < n){ while(right < n && arr[right] >= k){ right++; } int len = right - left; if(len - m + 1 > 0){ total += len - m + 1; } right++; left = right; } System.out.println(total); }
private static int MOD = 998244353; private static long total = 0; public static void prob3(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(), k = in.nextInt(), d = in.nextInt(); recursiveSum(n, k, d, false); System.out.println(total); } private static void recursiveSum(int sum, int k, int d, boolean ifDHasAppeared){ if( (sum == 0 && ifDHasAppeared) || (sum == d && !ifDHasAppeared)) { total = (total + 1) % MOD; } else { for(int i = 1; i <= k; i++){ if(i <= sum) { if(i >= d) { recursiveSum(sum - i, k, d, true); } else { recursiveSum(sum - i, k, d, ifDHasAppeared); } } else { break; } } } }
第四道,齿轮,感觉是道阅读理解题,但是我好像漏了一些case, 只过了55%
public static void prob4(){ int mod = 998244353; Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); String s = in.next(); long total = 0; for(int i = 0; i < n; i++){ char ch = s.charAt(i); if(ch == 'Z') { continue; } if(i == 0) { if(s.charAt(i + 1) != 'A'){ total = (total + 1) % mod; } } else if(i == n - 1) { if(s.charAt(i - 1) != 'A') { total = (total + 1) % mod; } }else{ // 按第一个按钮 if(s.charAt(i - 1) != 'A'){ total = (total + 1) % mod; } // 按第二个按钮 if(s.charAt(i + 1) != 'A'){ total = (total + 1) % mod; } } } // 不按按钮 System.out.println((total + 1) % mod); } }
第五道,考java基础掌握能力,一道简单的排序题,AC
static class Event{ int id; int priority; int necessity; public Event(int id, int pri, int nec){ this.id = id; this.priority = pri; this.necessity = nec; } } public static void prob5() { Scanner in = new Scanner(System.in); int n = in.nextInt(); Event[] events = new Event[n]; for(int i = 0; i < n; i++) { events[i] = new Event(i + 1, in.nextInt(), in.nextInt()); } Arrays.sort(events, (a, b)-> { if (b.necessity != a.necessity) { return (b.necessity - a.necessity); }else{ return (b.priority - a.priority); } }); for(int i = 0; i < n; i++){ if(i != n - 1) System.out.print(events[i].id + " "); else System.out.println(events[i].id); } }求大佬指点我没有AC的题