处理:
1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并)
2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并)
3.输入的文件可能带路径,记录文件名称不能带路径
数据范围:输入错误记录数量满足 ,每条记录的长度满足
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
文件路径为windows格式
如:E:\V1R2\product\fpgadrive.c 1325
将所有的记录统计并将结果输出,格式:文件名代码行数数目,一个空格隔开,如: fpgadrive.c 1325 1
结果根据数目从多到少排序,数目相同的情况下,按照输入第一次出现顺序排序。
如果超过8条记录,则只输出前8条记录.
如果文件名的长度超过16个字符,则只输出后16个字符
E:\V1R2\product\fpgadrive.c 1325
fpgadrive.c 1325 1
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Map<String, Integer> map = new LinkedHashMap<>(); while (sc.hasNextLine()) { String s = sc.nextLine(); if(s == null || "".equals(s)) break; String[] split = s.split("\\s"); String key = split[0].substring(split[0].lastIndexOf('\\') + 1) + " " + split[1]; map.put(key, map.containsKey(key) ? map.get(key) + 1 : 1); } List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return o2.getValue().compareTo(o1.getValue()); } }); for (int i = 0; i < 8; i ++) { String[] split = list.get(i).getKey().split("\\s"); if(split[0].length() > 16) split[0] = split[0].substring(split[0].length() - 16); System.out.println(split[0] + " " + split[1] + " " + list.get(i).getValue()); } } }
#include<iostream> #include<string.h> #include<memory.h> using namespace std; struct SRecord { int cnterror; int lineNum; char fileName[17]; char fileNameNumstr[40]; SRecord *next; }; int Insert(SRecord *head,SRecord *pSR) { SRecord *cur=head->next; SRecord *preCur=head; while (cur) { if(!strcmp(cur->fileNameNumstr,pSR->fileNameNumstr)) { cur->cnterror++; return 0; } preCur=cur; cur=cur->next; } pSR->cnterror++; preCur->next=pSR; pSR->next=nullptr; return 1; } void print(SRecord *head) { if(!head->next) return ; SRecord *cur=head->next; SRecord *maxp=cur; SRecord *preMax=head; SRecord *preCur=head; while (cur) { if(cur->cnterror>maxp->cnterror) { preMax=preCur; maxp=cur; } preCur=cur; cur=cur->next; } cout<<maxp->fileName<<' '<<maxp->lineNum<<' '<<maxp->cnterror<<endl; preMax->next=maxp->next; delete maxp; } void desSR(SRecord *head) { SRecord *cur=head; head=head->next; delete cur; while (head) { cur=head; head=head->next; delete cur; } } int main() { char str[200]; SRecord *pSR; SRecord *head=new SRecord; head->next=nullptr; while (cin.getline(str,sizeof(str))) { if (!strlen(str)) { break; } char *token,*laststr; token=strtok(str,"\\"); laststr=token; while (token) { laststr=token; token=strtok(nullptr,"\\"); } pSR=new SRecord; memset(pSR,0,sizeof(SRecord)); strcpy(pSR->fileNameNumstr,laststr); token=strtok(laststr," "); char fileNamestr[40],*pfileNamestr=token; strcpy(fileNamestr,token); int fileNamelen=strlen(fileNamestr); if(fileNamelen>=17) { fileNamelen-=16; pfileNamestr+=fileNamelen; } strcpy(pSR->fileName,pfileNamestr); token=strtok(nullptr," "); pSR->lineNum=atoi(token); //cout<<laststr<<endl<<pSR->fileName<<endl<<pSR->lineNum<<endl; Insert(head,pSR); } for(int i=0;i<8;++i) print(head); desSR(head); //system("PAUSE"); return 0; }
#include <stdio.h> #include <iostream> #include <string.h> #include <stdlib.h> using namespace std; typedef struct re{ char docinfo[100];//文件名 char ronu[10];//错误行号 int docdisplaypos;//要从哪里开始展示文件 int repos;//错误的初始位置 int errcnt;//错误数量 }Record; void quicksort(Record re[],int start,int last){ if(start>=last){ return ; } int left = start,pos=left+1,right=last; Record pivot = re[start],tmp; while(left != right){ if(re[pos].errcnt < pivot.errcnt){ tmp = re[right]; re[right--] = re[pos]; re[pos] = tmp; }else if(re[pos].errcnt > pivot.errcnt){ re[left++] = re[pos++]; }else{ if(re[pos].repos<pivot.repos){ re[left++] = re[pos++]; }else{ tmp = re[right]; re[right--] = re[pos]; re[pos] = tmp; } } } re[left] = pivot; quicksort(re,start,left-1); quicksort(re,left+1,last); } /*测试用例 E:\V1R2\product\fpgadrivea.c1111 1325 E:\V1R2\product\fpgadrivea.c11112 125 E:\V1R2\product\fpgadrive.c 1325 E:\V1R2\productss\fpgadrive.c 1325 E:\V1R2\product\fpgadrivea.c11112 1335 */ Record reco[100]; int main(void){ char s[300],rownum[10]; char *pdoc; int doclength; int recnt = 0; int hasflag; while(cin>>s>>rownum){ pdoc = strrchr(s,'\\'); hasflag = 0; for(int i = 0; i<recnt;i++){ if( 0 == strcmp(pdoc+1,reco[i].docinfo)){ if( 0 == strcmp(rownum,reco[i].ronu)){ reco[i].errcnt++; hasflag = 1; break; } } } if( 1 == hasflag){ continue; } strcpy(reco[recnt].docinfo,pdoc+1); strcpy(reco[recnt].ronu,rownum); reco[recnt].repos = recnt; doclength = strlen(reco[recnt].docinfo); if( doclength > 16 ){ reco[recnt].docdisplaypos = doclength-16; }else{ reco[recnt].docdisplaypos = 0; } reco[recnt].errcnt = 1; recnt++; } quicksort(reco,0,recnt-1); recnt = recnt>8?8:recnt; for(int i=0;i<recnt;i++){ printf("%s %s %d\n",&reco[i].docinfo[reco[i].docdisplaypos],reco[i].ronu,reco[i].errcnt); } return 0; }
from collections import OrderedDict def solution(lines): records = OrderedDict() # 有序字典,便于后期稳定输出 for e in lines: line = e.split() name = line[0].split('\\')[-1] if (name, line[1]) not in records:records[(name, line[1])] = 1 else:records[(name, line[1])] += 1 return records import sys try: while True: lines = sys.stdin.readlines() # print lines if not lines: break ret = solution(lines) # print ret #result = zip(ret.keys(), ret.values()) result = sorted(ret.iteritems(),key=lambda x:x[1], reverse=True) # 本身按递增排序的结果就是不稳定的,reverse之后就满足了。。 # print result # print result[:8] for e in result[:8]: if len(e[0][0]) > 16:print e[0][0][-16:],e[0][1],e[1] else: print e[0][0],e[0][1],e[1] except: # print result pass
大家帮忙运行一下我的程序,为什么能得出结果,但是提交的时候说是程序有错误? package com.huawei.subject; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class RecordError { private static Map<String, Object> map; private static List<Map<String, Object>> list; public static void main(String args[]) { Scanner sc = new Scanner(System.in); String file, fileName, midFileName; int errorLine, errorCount, lastIndex; list = new ArrayList<Map<String, Object>>(); while (sc.hasNext()) { map = new HashMap<String, Object>(); file = sc.next(); // 输入的原始文件名(包括路径) if (file.equals("quit")) { //跳出循环 break; } StringBuilder sb = new StringBuilder(); lastIndex = file.lastIndexOf("\\"); sb.append(file.substring(lastIndex + 1)); fileName = sb.toString(); // System.out.println("---"+file); // if (midFileName.lastIndexOf(" ") - 16 > 0) {// // 如果文件名超过16位。则去后16位 // lastIndex = midFileName.lastIndexOf(" ") - 16; // fileName = midFileName.substring(lastIndex); // } else { // fileName = midFileName; // 文件名 // } errorLine = sc.nextInt(); errorCount = 1; int flag = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i).get("fileName").toString().equals(fileName) && Integer.parseInt(list.get(i).get("errorLine").toString()) == errorLine) { errorCount = Integer.parseInt(list.get(i).get("errorCount").toString()) + 1; list.get(i).put("errorCount", errorCount); flag = 1; } } // map.put("errorCount", errorCount); if (flag == 0) { map.put("fileName", fileName); map.put("errorLine", errorLine); map.put("errorCount", errorCount); list.add(map); } } sc.close(); for (int i = 0; i < list.size(); i++) { midFileName = list.get(i).get("fileName").toString(); if (midFileName.lastIndexOf(" ") - 16 > 0) {// 如果文件名超过16位。则去后16位 lastIndex = midFileName.lastIndexOf(" ") - 16; fileName = midFileName.substring(lastIndex); list.get(i).put("fileName", fileName); } } Comparator<Map<String, Object>> cmp = new Comparator<Map<String, Object>>() { @Override public int compare(Map<String, Object> m1, Map<String, Object> m2) { return Integer.parseInt(m1.get("errorCount").toString()) - Integer.parseInt(m2.get("errorCount").toString()) == 0 ? 0 : Integer.parseInt(m2.get("errorCount").toString()) - Integer.parseInt(m1.get("errorCount").toString()); } }; Collections.sort(list, cmp); if(list.size()<=8){ for (Map map : list) { System.out.print(map.get("fileName") + " " + map.get("errorLine") + " " + map.get("errorCount")+" "); } }else{ for(int i=0;i<8;i++){ System.out.print(list.get(i).get("fileName") + " " + list.get(i).get("errorLine") + " " + list.get(i).get("errorCount")+" "); } } } }
/**
* 好像Java答案不多,我来献丑吧。
* 正如大家之前所说,这个题主要是考细节。所以我只说说代码的细节:
* 1. 读到文件路径,先用substring截取文件名。
* 2. 用Entry对象保存单个记录和出现次数。
* 3. 用Map来区分是否为新纪录,这要求Entry重新equals和hashCode。
* 4. 真正使用的是LinkedHashMap,使得遍历key时可以按照插入顺序遍历,满足了题目要求。
* 5. Arrays.sort对引用类型采用插入排序+归并排序,保证了排序稳定性,满足了题目要求。
* 6. Entry实现了Comparable接口,其实更好是写一个Comparator,因为是刷题无所谓了。
* 7. 对文件名长度的判定在Entry的toString中,这样写不好,因为是刷题无所谓了。
**/
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
HashMap<Entry, Entry> records = new LinkedHashMap<Entry , Entry>();
while(scan.hasNext()){
String name = scan.next();
int lines = scan.nextInt();
int index = name.lastIndexOf('\\');
//截取文件名
if(index!=-1)
name = name.substring(index+1);
//生成Entry
Entry entry = new Entry(name,lines);
if(records.containsKey(entry)){
//合并
Entry old = records.get(entry);
old.count++;
}else{
//新的
records.put(entry, entry);
}
} scan.close(); //获得结果
Entry[] result = new Entry[records.size()];
result = records.keySet().toArray(result);
Arrays.sort(result);
//输出结果
int size = Math.min(8, result.length);
for(int i=0;i<size;i++){
System.out.println(result[i]);
}
}
private static class Entry implements Comparable<Entry>{
public String name;
public int lines;
public int count;
public Entry(String n , int l){
this.name = n;
this.lines = l;
this.count=1;
}
@Override
public int compareTo(Entry e) {
if(this.count<e.count)
return 1;
else if(this.count == e.count)
return 0;
else
return -1;
}
@Override
public int hashCode(){
return name.hashCode()+lines;
}
@Override
public boolean equals(Object obj){
if(obj==this)
return true;
if(obj instanceof Entry){
Entry e = (Entry)obj;
if(e.name.equals(this.name)&&e.lines==this.lines)
return true;
}
return false;
}
@Override
public String toString(){
if(name.length()<=16)
return name+" "+lines+" "+count;
else
return name.substring(name.length()-16,name.length())+" "+lines+" "+count;
}
}
}
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Scanner; class ByValueComparator implements Comparator<String>{ HashMap<String, Integer> base_map; public ByValueComparator(HashMap<String,Integer> map) { // TODO Auto-generated constructor stub this.base_map = map; } @Override public int compare(String str1, String str2) { // TODO Auto-generated method stub if(!base_map.containsKey(str1) || !base_map.containsKey(str2)){ return 0; } if(base_map.get(str1) < base_map.get(str2)){ return 1; }else{ //相等也要返回-1,否则在排序时不会把相等的值放到TreeMap中,若=0则新值替代原值 //为何相等是返回-1不是返回1,根据情况而定,返回1代表新值放到旧值前面,-1代表新值放在旧值后面 return -1; } } } public class juan1_q2 { public static void main(String[] ags){ HashMap<String, Integer> recMap = new HashMap<String, Integer>(); Scanner input = new Scanner(System.in); String file = ""; int errorLine = 0; int lastIndex = 0; String recName = null; while(input.hasNext()){ file = input.next(); errorLine = input.nextInt(); lastIndex = file.lastIndexOf("\\"); recName = (lastIndex < 0)?file:file.substring(lastIndex+1)+" "+errorLine; int count = 0; if(!recMap.containsKey(recName)){ recMap.put(recName, 1); }else{ count = recMap.get(recName); recMap.put(recName, count+1); } } input.close(); //--------以上统计,以下排序取值------------- ArrayList<String> keys = new ArrayList<String>(recMap.keySet()); ByValueComparator bvc = new ByValueComparator(recMap); Collections.sort(keys, bvc); for(int i = 0;i < (keys.size() > 8?8:keys.size());i++){ String key = keys.get(i); StringBuilder res = new StringBuilder(); lastIndex = key.lastIndexOf(" "); int value = recMap.get(key); if(lastIndex > 16){ res.append(key.substring(lastIndex-16)); }else{ res.append(key); } res.append(" "+value); System.out.println(res.toString()); } } } 以上代码有两个问题: 1.comparator理解问题,返回1代表str1会放在str2后面,-1代表str1放在str2前面,若相等应该是返回0. 2.没有考虑输入顺序的排序,因此要使用LinkedHashMap代替HashMap来保存输入顺序的信息 修改代码通过,如下: import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Scanner; class ByValueComparator implements Comparator<String>{ HashMap<String, Integer> base_map; public ByValueComparator(HashMap<String,Integer> map) { // TODO Auto-generated constructor stub this.base_map = map; } @Override public int compare(String str1, String str2) { // TODO Auto-generated method stub if(!base_map.containsKey(str1) || !base_map.containsKey(str2)){ return 0; } if(base_map.get(str1) < base_map.get(str2)){ return 1; }else if(base_map.get(str1) > base_map.get(str2)){ //从大到小排序,因此若str1<str2,则返回1,即str2会排在str1前面;str1>str2,返回-1,则str1排在str2前面 //从小到大排序,-1代表str1<str2;0代表==,不动位置;1代表str1>str2,str1放在str2后。 return -1; }else{ return 0; } } } public class juan1_q2 { public static void main(String[] ags){ LinkedHashMap<String, Integer> recMap = new LinkedHashMap<String, Integer>(); Scanner input = new Scanner(System.in); String file = ""; int errorLine = 0; int lastIndex = 0; String recName = ""; while(input.hasNext()){ file = input.next(); errorLine = input.nextInt(); lastIndex = file.lastIndexOf("\\"); recName = (lastIndex < 0)?file:file.substring(lastIndex+1)+" "+errorLine; int count = 0; if(!recMap.containsKey(recName)){ recMap.put(recName, 1); }else{ count = recMap.get(recName); recMap.put(recName, count+1); } } input.close(); //--------以上统计,以下排序取值------------- ArrayList<String> keys = new ArrayList<String>(recMap.keySet()); System.out.println(keys.toString()); ByValueComparator bvc = new ByValueComparator(recMap); Collections.sort(keys, bvc); System.out.println(keys.toString()); for(int i = 0;i < (keys.size() > 8?8:keys.size());i++){ String key = keys.get(i); StringBuilder res = new StringBuilder(); lastIndex = key.lastIndexOf(" "); int value = recMap.get(key); if(lastIndex > 16){ res.append(key.substring(lastIndex-16)); }else{ res.append(key); } res.append(" ").append(value); System.out.println(res.toString()); } } }
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; bool compare(pair<string, int> a, pair<string, int> b){ return a.second > b.second; } int main(void){ string input, file; vector<pair<string, int>> errors; while (getline(cin, input)){ if (input.size() == 0) break; unsigned int f = input.rfind('\\'); file = input.substr(f + 1); errors.push_back(make_pair(file, 1)); for (int i = 0; i<(errors.size() - 1); i++){ if (errors[i].first == file){ errors[i].second++; errors.pop_back(); break; } } } stable_sort(errors.begin(), errors.end(), compare); int idx = 0; while (idx<8 && idx<errors.size()){ string check = errors[idx].first; int t = check.find(' '); if (t>16) errors[idx].first.erase(0, t - 16); cout << errors[idx].first << ' ' << errors[idx].second << endl; idx++; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<ErrorLine> list = new ArrayList<>(); while (scanner.hasNext()) { String line = scanner.nextLine(); int id = line.lastIndexOf('\\'); line = id<0 ? line : line.substring(id+1); boolean t = false; for (ErrorLine e : list) { if (line.equals(e.line)) { t = true; e.count++; } } if (!t) { list.add(new ErrorLine(line, 1)); } t = false; } scanner.close(); Collections.sort(list); int n = list.size() >= 8 ? 8 : list.size(); for (int i = 0; i < n; i++) { String[] s = list.get(i).line.split(" "); s[0] = s[0].length()>16 ? s[0].substring(s[0].length()-16) : s[0]; System.out.println(s[0] + " " + s[1] + " " + list.get(i).count); } } } class ErrorLine implements Comparable<ErrorLine> { public String line; public int count; public ErrorLine(String line, int count) { this.line = line; this.count = count; } @Override public int compareTo(ErrorLine e) { return e.count - this.count; } }
您的代码已保存
运行错误:请检查是否存在数组、列表等越界非法访问,内存非法访问等情况
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 18446744073709551614) > this->size() (which is 17)
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int outNum = 8; // 输出记录的数目 int nameLen = 16; //输出名字字符长度的上限 struct ErrRec{ string s_Name; string s_LineNum; int s_Count = 0; }; // 获取接收信息 vector<ErrRec>getInfo(vector<ErrRec>& result){ string line; while (getline(cin, line)){ int spaceIndex = line.find(' '); int slashIndex = line.find_last_of('\\'); ErrRec individual; string lineNum = line.substr(spaceIndex + 1); string name = line.substr(slashIndex + 1, spaceIndex - slashIndex - 1); if (result.size() == 0){ individual.s_LineNum = line.substr(spaceIndex + 1); individual.s_Name = line.substr(slashIndex + 1, spaceIndex - slashIndex - 1); individual.s_Count = 1; result.push_back(individual); } else{ bool sign = false; // 遍历ErrRec的所有数据,判断当前记录是否已经被记录 for (vector<ErrRec>::iterator it = result.begin(); it != result.end(); it++){ // 当前错误已经被记录,计数+1 if ((*it).s_LineNum == lineNum && (*it).s_Name == name){ (*it).s_Count++; sign = true; break; } } // 当前错误未被记录,在vector末端存入当前记录 if (!sign){ individual.s_LineNum = line.substr(spaceIndex + 1); individual.s_Name = line.substr(slashIndex + 1, spaceIndex - slashIndex - 1); individual.s_Count = 1; result.push_back(individual); } } } return result; } // 排序的自定义降序规则 bool compareRule(ErrRec a, ErrRec b){ return a.s_Count > b.s_Count; } // 输出当前vector中的错误记录 void printResult(vector<ErrRec>& result){ int printNum = 0; for (vector<ErrRec>::iterator it = result.begin(); it != result.end(); it++){ // 判断当前记录的数目是否超过outNum,若超过则只输出前8个 if (printNum == outNum){ break; } // 判断it指向的错误记录的名字是否超过16个字符,若超过则作截断 else{ if ((*it).s_Name.length() > nameLen){ int pose = nameLen - (*it).s_Name.length() - 1; string blockName = (*it).s_Name.substr(pose, nameLen); cout << blockName << " " << (*it).s_LineNum << " " << (*it).s_Count << endl; } else cout << (*it).s_Name << " " << (*it).s_LineNum << " " << (*it).s_Count << endl; } printNum++; } } int main(){ vector<ErrRec> result; result = getInfo(result); stable_sort(result.begin(), result.end(), compareRule); printResult(result); system("pause"); return 0; }
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> PII; #define x first #define y second struct cmp { bool operator()(const PII &p1,const PII &p2) { if(p1.x==p2.x) return p1.y<p2.y; return p1.x>p2.x; } }; int main() { unordered_map<string,unordered_map<int,PII>> mp; string s; int line,cnt=0; while(cin>>s>>line) { int pos=s.rfind("\\",s.size()-1); if(pos!=string::npos) { s=s.substr(pos+1); } if(!mp.count(s)) { mp[s][line].x++; mp[s][line].y=cnt++; } else if(mp.count(s) && !mp[s].count(line)) { mp[s][line].x++; mp[s][line].y=cnt++; } else if(mp.count(s) && mp[s].count(line)) { mp[s][line].x++; } } map<PII,pair<string,int>,cmp> _map; for(auto &p:mp) { for(auto &pson:p.y) { _map.insert({pson.y,{p.x,pson.x}}); } } int num=0; for(auto &p:_map) { if(num>=8) break; string &str=p.y.x; if(str.size()>16) cout<<str.substr(str.size()-16); else cout<<str; cout<<" "<<p.y.y<<" "<<p.x.x<<endl; num++; } return 0; }
def judge_in(str_in,ans): //判断相同的错误记录函数,返回布尔值以及当前出现次数
for i in ans:
if str_in in i[0]:
return [True,i[1]]
return [False,0]
try:
ans = []
while True: //坑爹的判题器,无限接收样例输入
ans_str = ''
line = input().split()
road = line[0]
code = int(line[1])
temp = road.split("\\")
filename = temp[-1]
sym = judge_in(filename+' '+str(code),ans)
if sym[0]: //取布尔类型的值做判断是否是重复错误
index = ans.index([filename+' '+str(code),sym[1]])
ans[index][1] +=1
else:
ans_str += filename + ' ' + str(code)
ans.append([ans_str,1])
except:
ans = sorted(ans,key=lambda x:x[1],reverse=True)//按照出现次数排序
for i in range(8):
tp = ans[i][0].split()
if len(tp[0])>16://判断字符是否不符合16个,进行差异化输出
print(tp[0][-16:],tp[1],ans[i][1])
else:
print(ans[i][0],ans[i][1])
此乃python3的强行破解法,按照题目要求一步一步的写import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); HashMap<String,Integer> hashmap = new HashMap<>(); List<String> arraylist = new ArrayList<>(); while(sc.hasNext()) { String[] info = sc.nextLine().split("\\\\"); String parseInfo = info[info.length - 1]; if(arraylist.contains(parseInfo)){ hashmap.put(parseInfo,hashmap.get(parseInfo)+1); }else{ arraylist.add(parseInfo); hashmap.put(parseInfo,1); } } int temp = 1; arraylist.sort(new Comparator<String>() { @Override public int compare(String o1, String o2) { if(hashmap.get(o1)<hashmap.get(o2)){ return 1; }else if(hashmap.get(o1)>hashmap.get(o2)){ return -1; }else{ if (arraylist.indexOf(o1)>arraylist.indexOf(o2)){ return 1; }else{ return -1; } } } }); sc.close(); for (String s : arraylist ){ if(temp>8){ break; } temp++; int length = s.split(" ")[0].length(); int cut_l = length - 16; int olength = s.length() - cut_l; if(length<=16){ System.out.println(s+" "+hashmap.get(s)); }else { System.out.println(s.substring(s.length()-olength,s.length())+" "+hashmap.get(s)); } } } }
sorted(res.items(), key=lambda x: x[1], reverse=True) #可以对字典res的values进行排序 (10, 3) > (10, 2) > (9, 5) (2956)# tuple的大小比较完整代码:
res = {} i = 0 while True: try: s = input() file_error = s.split('\\')[-1] found = False for key in res.keys(): if file_error in key: res[key] = (res[key][0]+1, res[key][1]) # 数量+1 found = True if not found: file_error_key = '{}'.format(file_error) res[file_error_key] = (1, 9999-i) (2957)# 把数量和出现次数放入键值 i += 1 except: break sort_res = sorted(res.items(), key=lambda x: x[1], reverse=True) for i in range(min(8, len(sort_res))): name, num = sort_res[i] name = name.split(' ') file_name, error = ''.join(name[:-1]), name[-1] file_name = file_name[-16:] print('{} {} {}'.format(file_name, error, num[0]))
var result = []; while (line = readline()) { var readline1 = line.split(' '); var temp = readline1[0].split('\\'); var filename = temp[temp.length - 1], codeNum = readline1[1]; var hasFlag =false; for(let i=0;i<result.length;i++){ if(result[i][0] == filename && result[i][1] == codeNum){ result[i][2] +=1; hasFlag =true; break; } } if(!hasFlag){ result.push([filename,codeNum,1]); } } // 排序 bSort(result); // 截取+输出 var len = result.length; if(len >8){ len = 8; } for(let i=0;i<len;i++){ result[i][0] =result[i][0].substr(-16); print(result[i].join(' ')); } // 冒泡,稳定排序 function bSort(arr) { var temp; for (let j = 0; j < arr.length - 1; j++) { for (let i = 0; i < arr.length - 1 - j; i++) { if (arr[i][2] < arr[i + 1][2]) { temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } } return arr; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i = 0; int falg = 0; String[] result = new String[8]; int[] count = new int[8]; while (sc.hasNextLine()) { String line = sc.nextLine(); if (line.equals("")) break; line = line.substring(line.lastIndexOf("\\") + 1); String[] ss = line.split(" "); for (int j = 0; j < 8; j++) { if (count[j] != 0) if (line.equals(result[j])) { count[j]++; falg = 1; } } if (falg == 0) { result[i] = line; count[i] = 1; i++; } } for (int j = 0; j < 8; j++) { if (count[j] != 0) if (result[j].substring(0,result[j].indexOf(" ")).length() <= 16) System.out.println(result[j] + " " + count[j]); else System.out.println(result[j].substring(result[j].length() - 16) + " " + count[j]); } } } //idea里输出正确这里输出顺序不一样,显示错误
用HashMap记录同文件名同错误行号的数目。import java.util.*;publicclassMain{//重写比较器,用于按次数排序public static class MyComparator implements Comparator<String> {@Overridepublicintcompare(String o1, String o2) {String[] arr1 = o1.split(" ");String[] arr2 = o2.split(" ");inta = Integer.valueOf(arr1[arr1.length - 1]);intb = Integer.valueOf(arr2[arr2.length - 1]);returnb - a;}}public static void main(String[] args) {Scanner scan = newScanner(System.in);ArrayList<String> resArrayList = new ArrayList<String>();ArrayList<String> arrayList = new ArrayList<String>();//保存同文件名同错误行号的数目HashMap<String, Integer> map1 = new HashMap<String, Integer>();while(scan.hasNext()) {String string = scan.nextLine();String pathAndName = string.split(" ")[0];introwNum = Integer.parseInt(string.split(" ")[1]);// 看文件名是否相同,找到最后一个/的位置intlastIndex = 0;for(inti = pathAndName.length() - 1; i >= 0; i--) {if(pathAndName.charAt(i) == '\\') {lastIndex = i;break;}}//文件名字+出错行号作为最终的文件名字String fileName = pathAndName.substring(lastIndex+1, pathAndName.length()) + " "+ rowNum;if(map1.containsKey(fileName)) {map1.put(fileName, map1.get(fileName) + 1);//arrayList.add(fileName);} else{map1.put(fileName, 1);arrayList.add(fileName);}}for(inti = 0; i < arrayList.size(); i++) {String res = "";String fileString = arrayList.get(i).split(" ")[0];if(fileString.length() > 16) {res += fileString.subSequence(fileString.length() - 16, fileString.length()) + " "+ arrayList.get(i).split(" ")[1]+ " "+ map1.get(arrayList.get(i));resArrayList.add(res);} else{res += fileString + " "+ arrayList.get(i).split(" ")[1] + " "+ map1.get(arrayList.get(i));resArrayList.add(res);}}Collections.sort(resArrayList, new MyComparator());if(resArrayList.size() > 8) {for(inti = 0; i < 8; i++) {System.out.println(resArrayList.get(i));}//System.out.println(result);} else{for(inti = 0; i < resArrayList.size(); i++) {System.out.println(resArrayList.get(i));}}}}