第一题:一直卡在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) 回帖