首页 > 08.15携程笔试
头像
骑士逸
编辑于 2020-08-15 20:32
+ 关注

08.15携程笔试

第一题:一直卡在92%,原因未知
import java.util.*;
import java.util.stream.Collectors;
public void Main{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        int k = s.nextInt();
        TreeSet<Integer> treeSet = new TreeSet<>();
        for (int i = k; i >= 0; --i) {
            int value = a * i + b * (k - i);
            treeSet.add(value);
        }
        System.out.println(treeSet);
    }
}
第二题:没判断输入是否合法
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    static class WorkflowNode {
        String nodeId;
        int timeoutMillis;
        List<WorkflowNode> nextNodes;
        boolean initialised;

        public static WorkflowNode load(String value) {
            // Create head node;
            Map<String, WorkflowNode> map = new HashMap<>();
            WorkflowNode head = new WorkflowNode("HEAD", 0, null);
            map.put(head.nodeId, head);
            for (String nodeValue : value.split("\\|")) {
                String[] properties = nodeValue.split("\\`");
                WorkflowNode node = map.get(properties[0]);
                node.timeoutMillis = Integer.parseInt(properties[1]);
                node.initialised = true;
                // Check next nodes
                if (properties[2].equals("END")) {
                    continue;
                }
                node.nextNodes = Arrays.stream(properties[2].split(","))
                        .map(p -> new WorkflowNode(p, 0, null))
                        .collect(Collectors.toList());
                node.nextNodes.forEach(p -> map.put(p.nodeId, p));
                map.put(node.nodeId, node);
            }

            return head;
        }

        public WorkflowNode(String nodeId, int timeoutMillis, List<WorkflowNode> nextNodes) {
            this.nodeId = nodeId;
            this.timeoutMillis = timeoutMillis;
            this.nextNodes = nextNodes;
        }
    }


    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        WorkflowNode node = node = WorkflowNode.load(cin.next());
        //递归遍历
        System.out.println(fun(node));
    }

    TreeSet<Integer> set = new TreeSet<>();

    private static int fun(WorkflowNode node) {
        if (node != null) {
            if (node.nextNodes != null) {
                int max = Integer.MIN_VALUE;
                int res = 0;
                for (WorkflowNode n : node.nextNodes) {
                    res = node.timeoutMillis + fun(n);
                    if (max < res) {
                        max = res;
                    }
                }
                return max;
            } else {
                return node.timeoutMillis;
            }
        }
        return 0;
    }
}



全部评论

(3) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐