4.17华为笔试
第二题:
代码:前面细节没处理好,考完了才改好
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine() ;
String[] arr = str.split(" ") ;
int m = Integer.valueOf(arr[0]) ;
int n = Integer.valueOf(arr[1]) ;
Map<String,TreeNode> map = new HashMap<>() ;
for(int i=0;i<n;i++){
//获取一行记录
String temp = sc.nextLine() ;
String[] arrays = temp.split(" ") ;
//记录当前节点
TreeNode node ;
if(map.containsKey(arrays[0])){
node = map.get(arrays[0]) ;
if("0".equals(arrays[2])){
node.questionOne += Integer.valueOf(arrays[3]) ;
}else{
node.questionTwo += Integer.valueOf(arrays[3]) ;
}
}else{
if("0".equals(arrays[2])){//严重问题
node = new TreeNode(arrays[0],Integer.valueOf(arrays[3]),0) ;
}else{//一般问题
node = new TreeNode(arrays[0],0,Integer.valueOf(arrays[3])) ;
}
//保存当前节点
map.put(node.val,node) ;
}
//在父节点当中保存当前节点为子节点
if(!map.containsKey(arrays[1])){
map.put(arrays[1],new TreeNode(arrays[1],0,0)) ;
}
TreeNode parent = map.get(arrays[1]) ;
boolean flag = true ;
for(int j=0;j<parent.childrens.size();j++){
if(parent.childrens.get(j).val == node.val){
flag = false ;
break;
}
}
if(flag){
parent.childrens.add(node) ;
}
}
int ans = 0 ;
TreeNode root = map.get("*") ;
for(int i=0;i<root.childrens.size();i++){
TreeNode node = root.childrens.get(i) ;
int number = dfs(node) ;
//System.out.println(root.childrens.get(i).val + " " + number) ;
if(number > m){
ans++ ;
}
}
System.out.println(ans) ;
}
public static int dfs(TreeNode root){
if(root == null){
return 0 ;
}
int sum = 0 ;
for(int i=0;i<root.childrens.size();i++){
sum += dfs(root.childrens.get(i)) ;
}
sum += root.questionOne * 5 + 2 * root.questionTwo ;
return sum ;
}
}
class TreeNode{
String val ;
//严重问题
int questionOne ;
//一般问题
int questionTwo ;
LinkedList<TreeNode> childrens ;
public TreeNode(){
}
public TreeNode(String val,int questionOne,int questionTwo){
this.val = val ;
this.questionOne = questionOne ;
this.questionTwo = questionTwo ;
childrens = new LinkedList<>() ;
}
}
基恩士成长空间 428人发布