快手笔试A卷 4题100

前三题都是练手
第四题笔试结束上代码~

抱歉更新得晚了一点,写完吃饭去了……

第一题 比较版本号
package 快手.c0825.a;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        for (int i = 0; i < m; i++) {
            String versionA = scanner.next();
            String versionB = scanner.next();
            System.out.println(helper(versionA, versionB));
        }
    }

    private static boolean helper(String versionA, String versionB) {
        int[] va = parseVersion(versionA);
        int[] vb = parseVersion(versionB);
        for (int i = 0; i < 4; i++) {
            if (va[i] < vb[i]) {
                return true;
            } else if (va[i] > vb[i]) {
                return false;
            }
        }
        return false;
    }

    private static int[] parseVersion(String v) {
        String[] strings = v.split("\\.");
        int[] version = new int[4];
        for (int i = 0; i < Math.min(strings.length, 4); i++) {
            version[i] = Integer.parseInt(strings[i]);
        }
        return version;
    }
}

第二题 完美平方数变种

package 快手.c0825.b;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        for (int i = 0; i < m; i++) {
            int num = scanner.nextInt();
            Set<Integer> set = new HashSet<>();
            boolean res = helper(num, set);
            System.out.println(res);
        }
    }

    private static boolean helper(int num, Set<Integer> set) {
        if (num == 1) {
            return true;
        }
        if (set.contains(num)) {
            return false;
        }
        set.add(num);
        int cur = 0;
        while (num != 0) {
            cur += (num % 10) * (num % 10);
            num /= 10;
        }
        return helper(cur, set);
    }
}

第三题 合并输入流
package 快手.c0825.c;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] a = scanner.nextLine().split(" ");
        String[] b = scanner.nextLine().split(" ");
        int count = Math.min(a.length / 4, b.length);
        int ia = 0, ib = 0;
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(a[ia++]);
                System.out.print(" ");
            }
            System.out.print(b[ib++]);
            System.out.print(" ");
        }
        while (ia < a.length) {
            System.out.print(a[ia++]);
            System.out.print(" ");
        }
        while (ib < b.length) {
            System.out.print(b[ib++]);
            System.out.print(" ");
        }
    }
}

第四题 树的好序列

这道题区分度大一点,大概就是总数减去纯红色连通域的个数,再就是记得取模
package 快手.c0825.d;

import java.util.*;


class Node {
    int label;
    List<Node> neighbors;
    List<Boolean> edgeColors;

    public static final Boolean RED = false;
    public static final Boolean BLACK = true;

    public Node(int label) {
        this.label = label;
        neighbors = new ArrayList<>();
        edgeColors = new ArrayList<>();
    }

    public static Boolean parseColor(int color) {
        return color == 1 ? BLACK : RED;
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        Node[] graph = new Node[n + 1];
        for (int i = 0; i < (n - 1); i++) {
            int e1 = scanner.nextInt();
            int e2 = scanner.nextInt();
            int color = scanner.nextInt();
            if (graph[e1] == null) {
                graph[e1] = new Node(e1);
            }
            if (graph[e2] == null) {
                graph[e2] = new Node(e2);
            }
            graph[e1].neighbors.add(graph[e2]);
            graph[e1].edgeColors.add(Node.parseColor(color));
            graph[e2].neighbors.add(graph[e1]);
            graph[e2].edgeColors.add(Node.parseColor(color));
        }
        System.out.println(helper(graph, k));
    }

    private static final int MOD = 1000000007;

    private static int helper(Node[] graph, int k) {
        int n = graph.length - 1;
        long total = pow(n, k);
        List<Integer> setList = new ArrayList<>();
        Set<Node> visited = new HashSet<>();
        Queue<Node> q = new LinkedList<>();
        for (int i = 1; i < graph.length; i++) {
            if (visited.contains(graph[i])) {
                continue;
            }
            Set<Node> redSet = new HashSet<>();
            redSet.add(graph[i]);
            visited.add(graph[i]);
            q.offer(graph[i]);
            while (!q.isEmpty()) {
                Node node = q.poll();
                for (int j = 0; j < node.neighbors.size(); j++) {
                    if (node.edgeColors.get(j) == Node.RED && !visited.contains(node.neighbors.get(j))) {
                        Node next = node.neighbors.get(j);
                        q.offer(next);
                        redSet.add(next);
                        visited.add(next);
                    }
                }
            }
            setList.add(redSet.size());
        }
        for (Integer size : setList) {
            total = (total + MOD - pow(size, k)) % MOD;
        }
        return (int) total;
    }

    private static long pow(int n, int k) {
        long total = 1;
        for (int i = 0; i < k; i++) {
            total = (total * n) % MOD;
        }
        return total;
    }
}






#快手##笔试题目##秋招#
全部评论
考试中不要发
点赞 回复 分享
发布于 2019-08-25 18:04
ac了三题 感觉够了就去吃饭了
点赞 回复 分享
发布于 2019-08-25 18:23
也是服了,第一题20%,漏看了最早出现的图片位置不变
点赞 回复 分享
发布于 2019-08-26 10:42
大佬np
点赞 回复 分享
发布于 2019-08-26 08:13
有题目么
点赞 回复 分享
发布于 2019-08-26 00:30
好厉害
点赞 回复 分享
发布于 2019-08-26 00:07
请问第一题有c++的代码嘛?我不能完全ac,哭了。。。
点赞 回复 分享
发布于 2019-08-25 21:45
这些题力扣上有吗
点赞 回复 分享
发布于 2019-08-25 20:36
BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int m=Integer.parseInt(in.readLine()); for(int i=0;i<m;i++) { String[] s=in.readLine().split(" "); String[] num1=s[0].split("\\."); String[] num2=s[1].split("\\."); int p=0,q=0; int len1=num1.length; int len2=num2.length; boolean re=true; int n1,n2; int eq=0; //System.out.println(Arrays.toString(num2)); for(int j=0;j<4;j++) { n1=p<len1?Integer.valueOf(num1[p]):0; n2=q<len2?Integer.valueOf(num2[q]):0; if(n1>n2) { re=false; break; } if(n1==n2) eq++; p++; q++; } re=eq==4?false:re; System.out.println(re); } 我的0AC为啥?
点赞 回复 分享
发布于 2019-08-25 18:47
第二题,各位数字平方和那个,哪位大佬能给讲讲思路?
点赞 回复 分享
发布于 2019-08-25 18:40
思路!
点赞 回复 分享
发布于 2019-08-25 18:39
大佬是开发还是算法
点赞 回复 分享
发布于 2019-08-25 18:38
大佬,求一下第四题代码。
点赞 回复 分享
发布于 2019-08-25 18:36
感觉大家都能做完前3题……那么,第4题才是真的有区分度😳😳
点赞 回复 分享
发布于 2019-08-25 18:36
看看你的代码吧
点赞 回复 分享
发布于 2019-08-25 18:35
这么厉害么
点赞 回复 分享
发布于 2019-08-25 18:33
来 来 来
点赞 回复 分享
发布于 2019-08-25 18:32
人均a3
点赞 回复 分享
发布于 2019-08-25 18:30
第四题思路: 红表示可以连通,黑表示不可以连通,求连通图个数和每个连通图元素的数量,然后答案公式: n^k-每个连通图元素个数^k的和-黑色节点个数
点赞 回复 分享
发布于 2019-08-25 18:29
好序列的那个???
点赞 回复 分享
发布于 2019-08-25 18:29

相关推荐

水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
醉蟀:你不干有的是人干
点赞 评论 收藏
分享
评论
8
58
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务