荣耀笔试 3道题,2道没有过。。。

1 题 加减法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.next();
        boolean flag = true;
        int sum = 0;
        int i = 0;
        while(i < line.length()) {
            if (line.charAt(i) == '-') {
                flag = false;
                ++i;
            } else if (line.charAt(i) == '+') {
                flag = true;
                ++i;
            } else {
                int num = 0;
                while(i < line.length() && line.charAt(i) != '-' && line.charAt(i) != '+') {
                    num = num * 10 + line.charAt(i++) - '0';
                }
                sum += flag ? num: -num;
            }
        }
        System.out.println(sum);
    }
}

2 题 步数排优。不知道改怎么输出了,通过了50%
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String[] noResults = {"excellent is null", "excellent is null", "good is null", "bad is null", "There is no data."};
        boolean[] hasResult = new boolean[4];
        Scanner sc = new Scanner(System.in);
        List<Entity> list = new ArrayList<>();
        while (sc.hasNextLine()) {
            list.add(new Entity(sc.nextLine()));
        }
        list.sort(null);
        for (int i = 0; i < list.size(); ++i) {
            Entity entity = list.get(i);
            hasResult[entity.level] = true;
            System.out.println(entity.say());
        }
        hasResult[1] |= hasResult[0];
        for (int i = 1; i < hasResult.length; ++i) {
            hasResult[0] |= hasResult[i];
            if (!hasResult[i]) {
                System.out.println(noResults[i]);
            }
        }
        if (!hasResult[0]) {
            System.out.println(noResults[4]);
        }
    }

    static class Entity implements Comparable<Entity> {
        static String[] levelStr = new String[]{"excellent", "excellent", "good", "bad"};
        int level = 3;
        String name;
        int totalSteps;
        List<Integer> stepList = new ArrayList<>();
        Entity(String line) {
            String[] ss = line.split(":");
            name = ss[0];
            String[] steps = ss[1].split(" ");
            List<Integer> indexList = new ArrayList<>(); // > 3w
            int over1w = 0;  // > 1w
            int over5t = 0;  // (5000, 10000)
            for (int i = 0; i < steps.length; ++i) {
                int step = Integer.parseInt(steps[i]);
                totalSteps += step;
                stepList.add(step);
                if (step >= 30000) {
                    indexList.add(i);
                } else if (step >= 10000) {
                    over1w++;
                } else if (step >= 5000) {
                    over5t++;
                }
            }
            if (indexList.size() >= 4) {
                for (int i = 1; i < indexList.size(); ++i) {
                    if (indexList.get(i) - indexList.get(i-1) <= 4) {
                        break;
                    }
                    level = 0;
                    return;
                }
            }
            if (over1w >= 15) {
                level = 1;
                return;
            }
            if (over5t >= 15) {
                level = 2;
            }
        }


        public String say() {
            StringBuilder sb = new StringBuilder();
            sb.append(name).append(":").append(levelStr[level]).append(" ").append(totalSteps);
            return sb.toString();
        }

        @Override
        public int compareTo(Entity o) {
            return level == o.level ? o.totalSteps - totalSteps : level - o.level;
        }
    }
}
/*
Gsy:35000 0 0 0 0 36000 0 0 0 0 0 40000 0 0 0 0 32000
Wj:12000 12000 12000 12000 12000 12000 12000 0 12000 12000 12000 12000 0 12000 12000 12000 12000 12000 12000
Jww:2000
Zzc:6000 6000 6000 6000 0 6000 6000 6000 0 0 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000 6000
Dbw:3000
 */

3 题 拓扑排序,检测有环。只能通过90.91%(在不检测环的情况下,添加了检测环,还是90.91%)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        int[] deg = new int[27];
        int[] hash = new int[128];
        int id = 0;
        char[] id2char = new char[27];
        List[] edges = new List[line.length() + 3 >> 1];
        for (int i = 0; i < line.length(); i += 4) {
            char u = line.charAt(i);
            char v = line.charAt(i + 2);
            if (hash[u] == 0) {
                hash[u] = ++id;
                id2char[id] = u;
            }
            if (hash[v] == 0) {
                hash[v] = ++id;
                id2char[id] = v;
            }
            if (edges[hash[u]] == null) {
                edges[hash[u]] = new ArrayList<>();
            }
            edges[hash[u]].add(hash[v]);
            deg[hash[v]]++;
        }
        int n = id; // [1..n]
        Queue<Integer> que = new PriorityQueue<>(); // 改为优先队列,正确率从20+%到90.91%
        for (int i = 1; i <= n; ++i) {
            if (deg[i] == 0) {
                que.add(i);
            }
        }
        StringBuilder cache = new StringBuilder();
        while(!que.isEmpty()) {
            int uid = que.remove();
            cache.append(id2char[uid]).append(";");
            if (edges[uid] == null) {
                continue;
            }
            List<Integer> toList = (ArrayList<Integer>)edges[uid];
            for (int vid: toList) {
                if (--deg[vid] == 0) {
                    que.add(vid);
                }
            }
        }
        for (int uid = 1; uid <= n; ++uid) {
            if (deg[uid] != 0) { // 存在环
                for (int vid = uid + 1; vid <= n; ++vid) {
                    List<Integer> toList = (ArrayList<Integer>)edges[vid];
                    int idx = Collections.binarySearch(toList, uid);
                    if (idx >= 0) {
                        System.out.println(id2char[vid] + "|" + id2char[uid]);
                        return;
                    }
                }
                break;
            }
        }
        System.out.println(cache);
    }
}


#荣耀手机##笔经#
全部评论
卧槽,老哥牛批呀 我只ac了1.2道
1 回复
分享
发布于 2021-08-03 20:11
妈的,第二道题输出他就没说明白
点赞 回复
分享
发布于 2021-08-03 20:17
联想
校招火热招聘中
官网直投
老哥第二道怎么理解的啊,我觉得它那几条规则不完备,概括不全
点赞 回复
分享
发布于 2021-08-03 20:18
为啥我的题目和你的不一样,我6:20上去做的
点赞 回复
分享
发布于 2021-08-03 20:54
大佬,请问第二题的输入怎么处理的 为什么我一直跳不出下面这个循环呢? while (sc.hasNextLine()) {         sc.nextLine(); }
点赞 回复
分享
发布于 2021-08-03 22:13
老哥,请问荣耀笔试都有哪些题型,就3道编程吗?
1 回复
分享
发布于 2021-08-05 21:30
老哥,能具体讲一下题目吗
点赞 回复
分享
发布于 2021-08-05 21:54
有摄像头吗
点赞 回复
分享
发布于 2021-08-05 21:58
想问一下,软件测试岗也是三道编程题是吗
点赞 回复
分享
发布于 2021-08-05 21:58
楼主笔试完有消息吗
点赞 回复
分享
发布于 2021-08-05 22:45
请问楼主是Java开发吗?
点赞 回复
分享
发布于 2021-08-06 09:25
老哥,是校招笔试题的吗?
点赞 回复
分享
发布于 2021-08-06 23:53
老哥,荣耀笔试题目分值分布是怎么样的,是1题100,2题200,3题300么?
点赞 回复
分享
发布于 2021-08-20 22:04

相关推荐

投递腾讯等公司10个岗位
点赞 评论 收藏
转发
华为 池子泡半年 总包和华为13级一致,公积金10%,单人一室一厅公寓
点赞 评论 收藏
转发
点赞 21 评论
分享
牛客网
牛客企业服务