Java面试题(携程笔试)
程序员文章站
2022-04-15 18:35:51
8.15携程:1,有长度为a和b的2种规格的瓷砖,现从这些瓷砖中任取k块儿来铺路,请按递增的顺序输出所有可能的铺成道路的长度。输入描述:输入为3个数,每行一个数,第一个数表示a,第二个数表示b,第三个数表示k。输出描述:输出结果为一个数组,数组中的值从小到大排列。如[3,4,5,6],若数组为空,则输出[],若有相同的结果则去除重复的。如:输入:123输出:[3,4,5,6]public class Main{public static void main(String[] ar...
8.15携程:
1,有长度为a和b的2种规格的瓷砖,现从这些瓷砖中任取k块儿来铺路,请按递增的顺序输出所有可能的铺成道路的长度。
输入描述:
输入为3个数,每行一个数,第一个数表示a,第二个数表示b,第三个数表示k。
输出描述:
输出结果为一个数组,数组中的值从小到大排列。如[3,4,5,6],若数组为空,则输出[],若有相同的结果则去除重复的。
如:输入:
1
2
3
输出:
[3,4,5,6]
public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int k = sc.nextInt(); if(k == 0) System.out.println("[]"); 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); } }
或者:
package ctrip; import java.util.Scanner; public class P1 { static int[] divingBoard(int a, int b, int k) { if (k == 0) return new int[0]; if (a == b) { int[] result = new int[1]; result[0] = a*k; return result; } int[] result = new int[k+1]; if (a > b){ int t = a; a = b; b = t; } for (int i = 0; i <= k; i++) { result[i] = a *(k-i) +i*b; } return result; } }
2,
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; } }
本文地址:https://blog.csdn.net/chen772209/article/details/108028146