题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
#include <iostream> #include <cstring> #include<sstream> #include <string> #include <system_error> #include<vector> #include<cmath> using namespace std; struct ErrorRecord { public: string fileName; int errorCode; int quantity = 0; }; bool find(vector<ErrorRecord>&errorRecords,int code,string name) { for( decltype(errorRecords.size())i = 0;i < errorRecords.size();i++) { if(errorRecords[i].errorCode == code&& name == errorRecords[i].fileName) { errorRecords[i].quantity++; return true; } } return false; } int main() { string str; vector<string> inputs;//记录每行输入 vector<ErrorRecord> errorRecords;//整理好的输入 int len = 0;//记录输入 while(getline(cin,str)) { len++; inputs.push_back(str); } for(int i = 0;i < len; i++) { string temp; stringstream ss; ss << inputs[i]; vector<string> record; while(getline(ss,temp,' ')) { record.push_back(temp); } stringstream path; path<<record[0]; string pathstr; while(getline(path, pathstr,'\\')){} if(pathstr.size()>16) { string tempstr(pathstr,(pathstr.size()-16)); pathstr = tempstr; } int code=0; for(int j = 0,dijit = record[1].size()-1;j<record[1].size();j++) { code += (record[1][j]-48)*pow(10,dijit); dijit--; } if(!find(errorRecords,code,pathstr)) { ErrorRecord errorRecord; errorRecord.fileName = pathstr; errorRecord.errorCode = code; errorRecord.quantity = 1; errorRecords.push_back(errorRecord); } } if(errorRecords.size()<=8) { for(auto i:errorRecords) cout<<i.fileName<<" "<<i.errorCode<<" "<<i.quantity<<endl; } else { for(int i = errorRecords.size()-8;i<errorRecords.size();i++) { cout<<errorRecords[i].fileName<<" "<<errorRecords[i].errorCode<<" "<<errorRecords[i].quantity<<endl; } } } // 64 位输出请用 printf("%lld")