京东Java编程题
看了大佬发的题解很难受,为啥都是暴利,自己就AC不了,为啥都是用栈来解决,我还是AC0%,第一题确实没什么思路。第二题、第三题都是感觉很简单的题目,就是AC不了。求大佬的解答。
第二题:暴力,只过了10%,就是Y一个一个枚举。
public class JD2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); long[] arr = new long[N]; long maxValue = Long.MIN_VALUE; for (int i = 0; i < N; i++) { arr[i] = scanner.nextLong(); maxValue = Math.max(maxValue, arr[i]); } for (int i = 0; i < arr.length; i++) { boolean flag = false; for (long Y = 2; Y <= Math.sqrt(maxValue) + 1; Y += 2) { if (arr[i] % Y == 0 && arr[i] / Y % 2 == 1) {//判断X System.out.println(arr[i] / Y + " " + Y); flag = true; break; } } if (!flag) System.out.println("No"); } }
}
第三题:用stack模拟,遍历,遇到“(”就入栈,遇到“)”就出栈,如果栈为空就把“)”如栈,如果遍历结束后栈容量为2或者0,就说明可以交换成功。(但是我没有判断左右括号是否相等的情况)
import java.util.LinkedList;
import java.util.Scanner;
public class JD3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); String[] strings = new String[N]; for (int i = 0; i < N; i++) { strings[i] = scanner.next(); } for (int i = 0; i < N; i++) { String input = strings[i]; LinkedList<Character> stack = new LinkedList<>(); for (int j = 0; j < input.length(); j++) { if (input.charAt(j) == '(') { stack.add(input.charAt(i)); continue; } if (input.charAt(j) == ')') { if (stack.isEmpty()) { stack.add(input.charAt(i)); continue; } else { stack.removeLast(); } } } if (stack.size() == 2 || stack.size() == 0) { System.out.println("Yes"); } else { System.out.println("No"); } } }
}
我自己测试了一下:
真的挺难受的,也不知道是哪里错了。
#笔试题目#

哔哩哔哩公司氛围 763人发布