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);
        }
    }
}


#笔试题目##网易#
全部评论
大佬,怎么面向测试用例做题呀,是不是得先有思路
点赞 回复 分享
发布于 2020-09-12 17:16
面向测试用例😅
点赞 回复 分享
发布于 2020-09-12 17:16
老哥 也太强了
点赞 回复 分享
发布于 2020-09-12 17:18
是真的秀,这得何等的耐力和毅力啊!
点赞 回复 分享
发布于 2020-09-12 17:21

相关推荐

我即大橘:耐泡王
点赞 评论 收藏
分享
10-06 12:46
门头沟学院 Java
跨考小白:定时任务启动
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务