华为4.15笔试题思路
第一题62.5%可能没考虑不合法输入,第二题一顿折腾,写的太慢,来不及,感觉繁琐,应该是我方法有问题,各位有什么好思路,贴下代码学习下
import java.util.*; public class Test1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String input=sc.next(); String[] s=input.split("\\,+"); HashMap<String,Integer> map=new HashMap<>(); for (int i=0;i<s.length;i++){ map.put(s[i],map.getOrDefault(s[i],0)+1); } PriorityQueue<Map.Entry<String,Integer>> queue=new PriorityQueue <Map.Entry<String,Integer>>(new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { if (o1.getValue() != o2.getValue()) { return o2.getValue() - o1.getValue(); } else { return o1.getKey().compareTo(o2.getKey()); } } }); for (Map.Entry<String,Integer> entry:map.entrySet()) { queue.add(entry); } Map.Entry<String,Integer> res=queue.poll(); System.out.println(res.getKey()); } }
import java.util.*; public class Main { public static void Test2(String[] args) { Scanner sc=new Scanner(System.in); String key = sc.next(); String input=sc.next(); String[] s=input.split("\\],+"); s[s.length-1]=s[s.length-1].substring(0,s.length-1); boolean flag=false; for (int i=0;i<s.length;i++){ String[] inner=s[i].split("\\["); String[] info=inner[1].split("\\,+"); String[][] per=new String[3][2]; if (key==inner[0]){ for (int j=0;j<3;j++){ per[j]=info[j].split("\\=+"); } if ("addr"==per[0][0]&&"mask"==per[1][0] &&"val"==per[2][0]){ System.out.print(per[0][1]+" "+ per[1][1]+" "+ per[2][1]+"\r\n"); flag=true; } } } if (flag==false){ System.out.print("Fail"); } } }