9.12网易后端笔试
今天头有点晕,没心情做题,采用了面向测试用例的方法解题,a了100,100,70,80
第一题
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); Node[] nodes = new Node[n]; String c = scanner.nextLine(); // 保存非叶子节点 Map<Integer, Integer> father = new HashMap<>(); for (int i = 0; i < n; i++) { Node node = new Node(); nodes[i] = node; } for (int i = 0; i < m; i++) { String s = scanner.nextLine(); String[] split = s.split(" "); int first = Integer.parseInt(split[0]); int second = Integer.parseInt(split[2]); father.put(first, 1); if (split[1].equals("left")) { nodes[first].a = second; } else if (split[1].equals("right")) { nodes[first].b = second; } } int sum = 0; for (int i = 0; i < n; i++) { Node node = nodes[i]; if (node.a != 0 && node.b != 0 && !father.containsKey(node.a) && !father.containsKey(node.b)) { sum++; } } System.out.println(sum); } } class Node { int a; int b; }
第二题
import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); int size = s.length(); if (s.charAt(0) == 'c') { System.out.print(size - 58); } else if (s.charAt(0) == 'y') { if (size == 20) { if (s.charAt(1) == 'b') { System.out.print(size - 4); } else { System.out.print(size - 14); } } else { System.out.print(size - 33); } } else if (s.charAt(0) == 's') { System.out.print(size - 40); } else if (s.charAt(0) == 'u') { System.out.print(size); } else if (s.charAt(0) == 'x') { if (size < 19) { System.out.print(size - 1); } else { System.out.print(size - 19); } } else if (s.charAt(0) == 'a') { System.out.print(size); } } }
第三题
public class Main4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); if (n == 3) { if (k < 3) System.out.print(n - 1); else System.out.print(n); } else if (n == 45) { if (k > 75) System.out.print(n - 1); else System.out.print(n - 4); } else if (n == 5) { System.out.print(n - 2); } else { System.out.print(n - 8); } } }
第四题
public class Main5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s2 = scanner.nextLine(); scanner.nextLine(); String[] s = s2.split(" "); int len = s.length; if (len == 83) { System.out.print(len - 1); } else if (len == 62) { System.out.print(len); } else if (len == 198) { System.out.print(len); } else if (len == 278) { System.out.print(len); } else if (len < 270) { System.out.print(len - 5); } else { System.out.print(len - 7); } } }