华泰证券8.28笔试题解代码,本地ac,提交编译出错???
华泰证券8.28笔试题解代码
为啥第一题我的代码输入的nextLine()报错????
逻辑是没有问题的,在本地的idea上是可以ac的……
气死
代码如下,求大佬指正:
public static class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int k = 1;
while (true){
boolean over = false;//是-1?
boolean isZero = false;//是0?
int start = 0;
List<List<Integer>> arr = new ArrayList<>();
while (true){
String[] str = in.nextLine().split(" ");
if (str[0].equals("") || str.length == 0) {
over = true;//空行不管
break;
}
if (str[0].charAt(0) == '-'){
break;//out
}
for (int i = 0; i < str.length; i++) {
List<Integer> tmp = new ArrayList<>();//一对
int from = str[i].charAt(0) - '0';
if (from == 0) {
isZero = true;
break;
}
int to = str[i].charAt(2) - '0';
tmp.add(0);
tmp.add(from);
tmp.add(to);
arr.add(tmp);
start = to;//随便选个点
}
if (isZero) break;//遇到0,结束
}
if (isZero){//树OK?
huaTai23Pro1.Main.Graph graph = generatGrap(arr);
boolean ans = bfs(graph.nodes.get(1));//拿起始点
System.out.println(ans ? "Case "+ k +" is a tree" :
"Case "+ k +" is not a tree");
}
if (over) {
k++;//计数增加
continue;
}
}
}
//图,BFS给一个图的节点,然后开始遍历,而不是给整张图
public static boolean bfs(huaTai23Pro1.Main.Node head){
if (head == null) return true;
Queue<huaTai23Pro1.Main.Node> queue = new LinkedList<>();
HashSet<huaTai23Pro1.Main.Node> set = new HashSet<>();
queue.add(head);
set.add(head);
boolean ans = true;//默认是树
while (!queue.isEmpty()){
head = queue.poll();
if(head.in > 1) {
ans = false;
break;//不行了就
}
//全部直接邻居
for(huaTai23Pro1.Main.Node next:head.nexts){
if (!set.contains(next)){
//没遍历过才能继续装
queue.add(next);
set.add(next);
}
}
}
//就这么简单
return ans;
}
//基础的数据类型,得自个写
//边--和节点是相互渗透的俩数据结构
public static class Edge{
public int weight;
public huaTai23Pro1.Main.Node from;
public huaTai23Pro1.Main.Node to;
public Edge(int w, huaTai23Pro1.Main.Node a, huaTai23Pro1.Main.Node b){
weight = w;
from = a;
to = b;
}
}
//节点
public static class Node{
public int in;
public int out;
public int value;
public ArrayList<huaTai23Pro1.Main.Node> nexts;
public ArrayList<huaTai23Pro1.Main.Edge> edges;//这里是相互渗透的,既然有邻居,就右边
public Node(int v){
value = v;//只需要给这么一个value即可
in = 0;
out = 0;
nexts = new ArrayList<>();
edges = new ArrayList<>();
}
}
//图结构玩起来,图右边,节也有边
public static class Graph{
public HashMap<Integer, huaTai23Pro1.Main.Node> nodes;//v,Node
public HashSet<huaTai23Pro1.Main.Edge> edges;
public Graph(){
nodes = new HashMap<>();//一般节点有value,对应包装袋,都是用哈希表玩的,并查集就是这么玩的
edges = new HashSet<>();
}
public int getNodeNum(){
return nodes.size();
}
public int getEdgeNum(){
return edges.size();
}
}
//将非标准图结构转化为左神标准图结构
public static huaTai23Pro1.Main.Graph generatGrap(List<List<Integer>> matrix){
if (matrix == null || matrix.size() == 0) return null;
//matrix==
//[1,1,2]
//[2,2,3]
//[3,3,1],w,f,t
//挨个遍历行
huaTai23Pro1.Main.Graph graph = new huaTai23Pro1.Main.Graph();
for (int i = 0; i < matrix.size(); i++) {
//建节点和边,然后装图,将节点的入度,出度,边和邻居放好
int weight = matrix.get(i).get(0);
int from = matrix.get(i).get(1);
int to = matrix.get(i).get(2);
//图中没节点,建,否则不必重复搞
if (!graph.nodes.containsKey(from)) graph.nodes.put(from, new huaTai23Pro1.Main.Node(from));
if (!graph.nodes.containsKey(to)) graph.nodes.put(to, new huaTai23Pro1.Main.Node(to));
huaTai23Pro1.Main.Node fromNode = graph.nodes.get(from);
huaTai23Pro1.Main.Node toNode = graph.nodes.get(to);//有就拿出来
huaTai23Pro1.Main.Edge edge = new huaTai23Pro1.Main.Edge(weight, fromNode, toNode);//建边
graph.edges.add(edge);
fromNode.out++;
toNode.in++;
fromNode.nexts.add(toNode);
fromNode.edges.add(edge);//除了入度说终点,其余都是说原点
}
return graph;
}
} 第二题ac
class Solution: def upper_bound_(self , n , v , a ): if a[-1] < v: return len(a) + 1 l, r = 0, len(a) - 1 while l < r: mid = l + ((r - l)>>1) if a[mid] < v: l = mid + 1 else: r = mid return l + 1
public static class Main {
public int a = 10;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()){
int n = in.nextInt();
int[] ability = new int[n];
for (int i = 0; i < n; i++) {
ability[i] = in.nextInt();
}
int k = in.nextInt();
int d = in.nextInt();
long[][] maxProduct = new long[n][k];
long[][] minProduct = new long[n][k];
for (int i = 0; i < n; i++) {
maxProduct[i][0] = ability[i];
minProduct[i][0] = ability[i];
}
long max = Long.MIN_VALUE;
for (int i = 0; i < n; i++) {
for (int j = 1; j < k; j++) {
for (int p = i - 1; p >= Math.max(i - d, 0); p--) {
maxProduct[i][j] = Math.max(maxProduct[i][j], maxProduct[p][j - 1]);
maxProduct[i][j] = Math.max(maxProduct[i][j], maxProduct[p][j - 1] * ability[i]);
minProduct[i][j] = Math.min(minProduct[i][j], minProduct[p][j - 1]);
minProduct[i][j] = Math.min(minProduct[i][j], minProduct[p][j - 1] * ability[i]);
}
}
max = Math.max(max, maxProduct[i][k - 1]);
}
System.out.println(max);
}
}
}
查看4道真题和解析