【華為OJ19】簡單錯誤記錄
阿新 • • 發佈:2019-02-13
首先理解錯了題目意思,做法也很複雜:
參考了別人的做法:import java.util.Scanner; import java.util.Set; import java.util.TreeSet; /** * 開發一個簡單錯誤記錄功能小模組,能夠記錄出錯的程式碼所在的檔名稱和行號。 1、 記錄最多8條錯誤記錄,迴圈記錄,對相同的錯誤記錄(淨檔名稱和行號完全匹配)只記錄一條,錯誤計數增加; 2、 超過16個字元的檔名稱,只記錄檔案的最後有效16個字元; 3、 輸入的檔案可能帶路徑,記錄檔名稱不能帶路徑。 輸入描述:一行或多行字串。每行包括帶路徑檔名稱,行號,以空格隔開。 輸出描述:將所有的記錄統計並將結果輸出,格式:檔名 程式碼行數 數目,一個空格隔開,如: 輸入例子:E:\V1R2\product\fpgadrive.c 1325 輸出例子:fpgadrive.c 1325 1 * @author WGS */ public class Main { private static int errorItems=0; private static void RecordingErrorLogs(Set<String> set,String[] input) { //input[0]檔案路徑:E:\V1R2\product\fpgadrive.c input[1]行號:1325 String fileName=""; String rowNumStr=""; //1 獲取檔名,超過長度16的擷取 int index1=input[0].lastIndexOf("\\");//最後一個斜槓位置 int lenthOfFile=input[0].length()-index1-1; if(lenthOfFile>16){ index1=input[0].length()-16; } //2 獲取行號 rowNumStr=input[1]; String validStr= fileName +","+ rowNumStr; //3 驗證:對相同的錯誤記錄(淨檔名稱和行號完全匹配)只記錄一條,錯誤計數增加; if(!set.add(validStr)){ set.add(validStr); errorItems++;//有重複 } } private static String setToString(Set<String> set){ StringBuilder sb=new StringBuilder(256); for(String s:set){ String[] tempStr=s.split(","); sb.append(tempStr[0]).append(" ").append(tempStr[1]).append(" ").append(errorItems).append("\n"); } return sb.toString(); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); Set<String> set=new TreeSet<>(); while(sc.hasNext()){ for(int i=0;i<8;i++){ String[] input=sc.nextLine().split("\\s+"); RecordingErrorLogs(set,input); } System.out.println(setToString(set)); } sc.close(); } }
package com.oj.test; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); Map<String, Integer> map=new LinkedHashMap<String, Integer>(); while(sc.hasNext()){ String str=sc.next(); int linenum=sc.nextInt(); String[] arr=str.split("\\\\"); //根據\切割 String s=arr[arr.length-1]; if(s.length()>16) //擷取 s=s.substring(s.length()-16); String key=s+" "+linenum; int value=1; if(map.containsKey(key)) map.put(key, map.get(key)+1); else { map.put(key, value); } } int count=0; for(String string:map.keySet()){ count++; if(count>(map.keySet().size()-8)) //輸出最後八個記錄 System.out.println("***"+string+" "+map.get(string)); } } }
自己重新做了一遍,整理了一下思路:
package com.oj.exe2; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; /** * 開發一個簡單錯誤記錄功能小模組,能夠記錄出錯的程式碼所在的檔名稱和行號。 1、 記錄最多8條錯誤記錄,迴圈記錄,對相同的錯誤記錄(淨檔名稱和行號完全匹配)只記錄一條,錯誤計數增加; 2、 超過16個字元的檔名稱,只記錄檔案的最後有效16個字元; 3、 輸入的檔案可能帶路徑,記錄檔名稱不能帶路徑。 輸入描述:一行或多行字串。每行包括帶路徑檔名稱,行號,以空格隔開。 輸出描述:將所有的記錄統計並將結果輸出,格式:檔名 程式碼行數 數目,一個空格隔開,如: 輸入例子:E:\V1R2\product\fpgadrive.c 1325 輸出例子:fpgadrive.c 1325 1 * @author WGS */ public class Exe19_2 { private static void RecordingErrorLogs(Map<String, Integer> map,String fileLocation,int lineNum) { //1 獲取檔名,超過長度16的擷取 int index1=fileLocation.lastIndexOf("\\");//最後一個斜槓位置 int lenthOfFile=fileLocation.length()-index1-1; if(lenthOfFile>16){ index1=fileLocation.length()-16; } String fileName=fileLocation.substring(index1+1); //2 將檔案路徑和行號連線為字串,作為map的key鍵值 String inputStr= fileName +" "+ lineNum; //3 驗證:對相同的錯誤記錄(淨檔名稱和行號完全匹配)只記錄一條,錯誤計數增加; if(map.containsKey(inputStr)){ map.put(inputStr,map.get(inputStr)+1); }else{ //沒有重複的,次數就設定為1 map.put(inputStr,1); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); //不需要排序,所以使用LinkedHashMap Map<String, Integer> map=new LinkedHashMap<String, Integer>(); while(sc.hasNext()){ String fileLocation=sc.next(); int lineNum=sc.nextInt(); //輸一條資訊,記錄一條 RecordingErrorLogs(map,fileLocation,lineNum); } //遍歷map的key鍵值(含有檔案+行號資訊),輸出最後的八條記錄; int count=0; for(String s:map.keySet()){ count++; if(count>(map.keySet().size()-8))//從倒數第八條開始遍歷 System.out.println(s+" "+map.get(s));//檔案路徑行號+個數 } sc.close(); } }
結果在eclipse中能夠完全通過(使用他給的測試用例),牛客上只通過30%,考慮到////代表兩個//的意思,可能是輸入路徑中的“\”被轉義成"//"了,所以應該這樣:
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/**
* 開發一個簡單錯誤記錄功能小模組,能夠記錄出錯的程式碼所在的檔名稱和行號。
1、 記錄最多8條錯誤記錄,迴圈記錄,對相同的錯誤記錄(淨檔名稱和行號完全匹配)只記錄一條,錯誤計數增加;
2、 超過16個字元的檔名稱,只記錄檔案的最後有效16個字元;
3、 輸入的檔案可能帶路徑,記錄檔名稱不能帶路徑。
輸入描述:一行或多行字串。每行包括帶路徑檔名稱,行號,以空格隔開。
輸出描述:將所有的記錄統計並將結果輸出,格式:檔名 程式碼行數 數目,一個空格隔開,如:
輸入例子:E:\V1R2\product\fpgadrive.c 1325
輸出例子:fpgadrive.c 1325 1
* @author WGS
*/
public class Main {
private static void RecordingErrorLogs(Map<String, Integer> map,String fileLocation,int lineNum) {
//input[0]檔案路徑:E:\V1R2\product\fpgadrive.c input[1]行號:1325
//1 獲取檔名,超過長度16的擷取
String[] arr=fileLocation.split("\\\\");//分解:>>E: \\ V1R2 \\ product \\ fpgadrive.c
String fileName=arr[arr.length-1];
if(fileName.length()>16){
fileName=fileName.substring(fileName.length()-16);
}
//2 將檔案路徑和行號連線為字串,作為map的key鍵值
String inputStr= fileName +" "+ lineNum;
//3 驗證:對相同的錯誤記錄(淨檔名稱和行號完全匹配)只記錄一條,錯誤計數增加;
if(map.containsKey(inputStr)){
map.put(inputStr,map.get(inputStr)+1);
}else{
//沒有重複的,次數就設定為1
map.put(inputStr,1);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//不需要排序,所以使用LinkedHashMap
Map<String, Integer> map=new LinkedHashMap<String, Integer>();
while(sc.hasNext()){
String fileLocation=sc.next();
int lineNum=sc.nextInt();
//輸一條資訊,記錄一條
RecordingErrorLogs(map,fileLocation,lineNum);
}
//遍歷map的key鍵值(含有檔案+行號資訊),輸出最後的八條記錄;
int count=0;
for(String s:map.keySet()){
count++;
if(count>map.size()-8)//從倒數第八條開始遍歷
System.out.println(s+" "+map.get(s));//檔案路徑行號+個數
}
sc.close();
}
}