美团笔试题校招8-13代码
Java版仅供参考,ak
第一题
package Test1; import java.util.*; /** * @description: * @date: 2022-08-13 **/ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, t; n = in.nextInt(); t = in.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = in.nextInt(); } Arrays.sort(a); long cur = 0, ans = 0; for(int i = 1; i < n; i++) { if(cur + t > a[i]) { ans++; } else { cur += t; } } System.out.println(ans); } }
第二题
package Test2; import java.util.Scanner; /** * @description: * @date: 2022-08-13 **/ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, m , k; n = in.nextInt(); m = in.nextInt(); k = in.nextInt(); int[][]vis = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { vis[i][j] = 0; } } vis[0][0]=1; String str = in.next(); int x = 0, y = 0; int res = 1, ans = -1; //System.out.println(str); for(int i = 0; i < str.length(); i ++) { char c = str.charAt(i); if(c == 'W') { x--; }else if(c == 'S') { x++; }else if(c == 'A') { y--; } else { y++; } if(vis[x][y] == 0) { vis[x][y] = 1; res ++; } if(res == n * m) { ans = i + 1; break; } } if(res == n * m) { System.out.println("Yes"); System.out.println(ans); } else { System.out.println("No"); res = n * m -res; System.out.println(res); } } }
第三题
package Test3; import java.util.Deque; import java.util.LinkedList; import java.util.Scanner; /** * @description: * @date: 2022-08-13 **/ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Deque<Integer> deque; deque = new LinkedList<>(); for (int i = n - 1; i > -1 ; i--) { deque.addFirst(arr[i]); deque.addFirst(deque.pollLast()); deque.addFirst(deque.pollLast()); } for (Integer x: deque) { System.out.print(x + " "); } } }
第四题
package Test4; import java.util.*; /** * @description: * @date: 2022-08-13 **/ public class Main { // a[i] + a[k] == 3 * a[j] public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int ans = 0; int[] cnt = new int[100007]; for(int i = 0; i < n; i++) { Arrays.fill(cnt, 0); for (int k = i + 1; k < n; k++) { int j = arr[i] + arr[k]; if(j % 3 == 0){ ans += cnt[j / 3]; } cnt[arr[k]]++; } } System.out.println(ans); } }
第五题
package Test5; import java.util.Scanner; /** * @description: * @date: 2022-08-13 **/ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int ans = arr[0]; for (int i = 0; i < n; i++) { ans = Math.max(ans, arr[i]); if(i * 2 + 1 < n) { arr[i * 2 + 1] += arr[i]; } if(i * 2 + 2 < n) { arr[i * 2 + 2] += arr[i]; } } System.out.println(ans); } }#美团笔试题##美团##美团笔试##美团校招#