美团笔试08.13
这次题目很简单,但是没有全AC,心态有点爆炸
1.
public class Test06 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int time = sc.nextInt(); int[] times = new int[num]; for(int i=0;i<num;i++){ times[i]=sc.nextInt(); } Arrays.sort(times); long res = 0; int curtime = 0; for(int i=0;i<num;i++){ if(times[i]-curtime<time){ res++; }else{ curtime+=time; } } System.out.println(res); } }
2
public class Test07 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] nums = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); int m = nums[0]; int n = nums[1]; char[] ch = sc.nextLine().toCharArray(); boolean[][] arr= new boolean[nums[0]][nums[1]]; arr[0][0]=true; int x = 0; int y = 0; int last = n*m-1; boolean flag =false; for(int i=0;i<ch.length;i++) { if (ch[i] == 'W') { if (x >= 1) { x--; } } else if (ch[i] == 'S') { if (x < m - 1) { x++; } } else if (ch[i] == 'A') { if (y >= 1) { y--; } } else if (ch[i] == 'D') { if (y < n - 1) { y++; } } if (!arr[x][y]) last--; arr[x][y] = true; if (last == 0) { System.out.println("Yes"); System.out.println(i+1); flag =true; break; } } if (!flag) { System.out.println("No"); System.out.println(last); } } }
扑克 * 时间限制: 3000MS* 内存限制: 589824KB
- 题目描述:
- Alice和Bob在玩一个游戏。有n张卡牌,点数分别为1到n。进行洗牌后,n张牌从上到下叠放形成一个牌堆。每次Alice先将当前牌堆顶的一张牌放到牌堆底,然后Bob再将当前牌堆顶的一张牌放到牌堆底。(特别地,当牌堆中只有一张牌时,相当于不进行任何操作)接着,他们会翻开当前牌堆顶的牌,并记下它的点数。当所有牌都被翻开后,他们也记下了n个点数。现在他们想根据记下的这个序列来还原一开始的牌(从牌堆顶到牌堆底每一张牌的点数)。
- 输入描述
- 第一行是一个正整数n,表示有n张牌。
- 接下来一行n个用空格隔开的正整数,第i个数a_i表示第i张被翻开的牌的点数。
- 1<=n<=100000
- 输出描述
- 一行n个用空格隔开的正整数,第i个数表示初始牌堆中从牌堆顶到牌堆底的第i张牌的点数。
- 样例输入
- 4
- 1 2 3 4
- 样例输出
- 4 2 1 3
- 提示
- 样例解释1
- 初始牌堆为:4 2 1 3
- Alice和Bob分别操作后牌堆为:1 3 4 2,此时1被翻开,牌堆变为3 4 2
- Alice和Bob分别操作后牌堆为:2 3 4,此时2被翻开,牌堆变为3 4
- Alice和Bob分别操作后牌堆为:3 4,此时3被翻开,牌堆变为4
- 4.Alice和Bob分别操作后牌堆依旧为4,此时4被翻开。
public class Test08 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = sc.nextInt(); int[] card = new int[sum]; for(int i=0;i<sum;i++){ card[i]=sc.nextInt(); } Deque<Integer> q = new ArrayDeque<>(); q.push(card[sum-1]); q.push(card[sum-2]); for (int i=sum-3;i>=0;i--){ q.push(card[i]); q.push(q.pollLast()); q.push(q.pollLast()); } for (int i = 0; i < sum; i++) { System.out.print(q.poll()); System.out.print(" "); } } }
4 题解来自 大佬
public class Test09 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } long res = 0; Map<Integer, Long> m1 = new HashMap<>(); Map<Integer, Long> m2 = new HashMap<>(); for (int j = 1; j < n - 1; j++) { int aj3 = 3 * arr[j]; m1.clear(); m2.clear(); for (int i = j - 1; i >= 0; i--) { m1.put(arr[i], m1.getOrDefault(arr[i], 0L) + 1); } for (int k = j + 1; k < n; k++) { m2.put(arr[k], m2.getOrDefault(arr[k], 0L) + 1); } for (int ai : m1.keySet()) { long cnt = m1.get(ai) * m2.getOrDefault(aj3 - ai, 0L); res += cnt; } } System.out.println(res); } }
5
public class Test10 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int[] nums= new int[num+1]; for (int i=0;i<num;i++){ nums[i+1]=sc.nextInt(); } int res = dfs(1,nums); System.out.println(res); } public static int dfs(int index,int[] arr){ if(2*index>arr.length-1) return arr[index]; if(2*index+1>arr.length-1) return arr[index]; int left = dfs(2*index,arr); int right = dfs(2*index+1,arr); return arr[index]+Math.max(left,right); } }#美团笔试#