騰訊2020.4.26筆試


這次筆試給我的教訓是很多題目不要想當然,也不要想太多。像第一題、第三題,想太多就是問題;像最后一題,如果不是想太多也不會浪費太多時間。然后就是很多細節,比如並查集的細節就沒有太了解,導致花費了很多不必要的功夫。

第一題:


我發現我每次筆試的問題就是想太多……實際上這個題最簡單的方法就可以做出來。

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] array = new int[n][2];
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            array[i][0] = x;
            array[i][1] = y;
        }
        sc.close();
        int res = maxCoin(m, array);
        System.out.print(res);
    }

    private static int maxCoin(int m, int[][] array) {
        if (m == 0 || array.length == 0)
            return 0;
        int total_cost = 0;
        int total_coin = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i][0] / m > array[i][1]) {//只要打這個怪是賺的就打
                total_cost += array[i][0];
                total_coin += array[i][1];
            }
        }
        if (total_cost % m == 0) {
            total_coin -= (total_cost / m);
        } else {
            total_coin -= (total_cost / m + 1);
        }
        return total_coin;
    }

第二題:


純數學題,算就算了好久,不知道做的對不對。

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        sc.close();
        double res = compute(A, B, C);
        System.out.println(res);
    }

    private static double compute(int a, int b, int c) {
        if (a == 0 || a <= 2 * b * c)
            return 0;
        double v = 2 * Math.sqrt(a * a - 2 * a * b);
        double y1 = (2 * a + v) / (2 * b * b * 1.0);
        double y2 = (2 * a - v) / (2 * b * b * 1.0);
        double size = Math.abs(helper(y1, a, b, c) - helper(y2, a, b, c));
        return size;
    }

    private static double helper(double y, int a, int b, int c) {
        return (y * y / (2 * b)) - (c * y / b) - (y * y * y / (6 * a));
    }

第三題:


多想了公式算錯了可還行……

    private static final int mod = 100003;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();//2
        int n = sc.nextInt();//3
        sc.close();
        int res = chongtu(m, n);
        System.out.println(res);
    }

    private static int chongtu(int m, int n) {
        if (m == 0 || n == 0)
            return 0;
        int res = binaryPow(m, n) - m % mod * binaryPow(m - 1, n - 1);//所有方法減去完全不沖突的方法
        return res;
    }


    private static int binaryPow(int m, int n) {
        int ans = 1;
        while (n > 0) {
            if ((n & 1) != 0) {
                ans = ans * m % mod;//快速冪所有部分都要%mod,避免溢出
            }
            m = m * m % mod;
            n = n >> 1;
        }
        return ans;
    }

第四題:

第五題:


最后一秒寫出來沒來得及粘上去啊啊啊!!!!
哭唧唧……

    private static ArrayList<Integer> count;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            int n = sc.nextInt();
            int[][] array = new int[n][2];
            Set<Integer> set = new HashSet<>();
            for (int j = 0; j < n; j++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                set.add(x);
                set.add(y);
                array[j][0] = x;
                array[j][1] = y;
            }
            int res = pengyou(set.size(), array);
            System.out.print(res);
        }
        sc.close();
    }
    private static int pengyou(int n, int[][] array) {
        if (n < 2)
            return n;
        boolean[][] quan = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(quan[i], false);
            quan[i][i] = true;
        }
        for (int[] ints : array) {
            int x = ints[0] - 1;
            int y = ints[1] - 1;
            quan[x][y] = true;
            quan[y][x] = true;
        }
        boolean[] hasVisit = new boolean[n];
        Arrays.fill(hasVisit, false);
        count = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (!hasVisit[i]) {//DFS
                hasVisit[i] = true;
                dfs(quan, i, hasVisit, 1);
            }
        }
        int max = 1;
        for (int i = 0; i < count.size(); i++) {
            max = Math.max(max, count.get(i));
        }
        return max;
    }

    private static void dfs(boolean[][] quan, int x, boolean[] hasVisit, int sum) {
        boolean flag = true;
        for (int i = 0; i < quan.length; i++) {
            if (quan[i][x] && !hasVisit[i]) {
                flag = false;
                hasVisit[i] = true;
                sum++;
                dfs(quan, i, hasVisit, sum);
            }
        }
        if (flag)
            count.add(sum);
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM