华为2016校园招聘上机笔试题----简单错误记录

时间:2022-03-31 18:54:31
[编程题] 简单错误记录 开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。 
处理:
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
#include <iostream>
#include <string>
#include <algorithm>

using namespace::std ;

bool compare( pair<string, int> a, pair<string, int> b ) {
return a.second > b.second ;
}

int main() {
string input , file ;
vector<pair<string, int>> errors ;

while ( getline( cin, input ) ) {
if ( input.empty() == true ) break ;
file = input.substr( input.rfind( '\\' ) + 1 ) ;
errors.push_back( make_pair( file, 1 ) ) ; // 创建一个 pair
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 index = 0 ;
while ( index < 8 && index < errors.size() ) {
int fileLength = errors[index].first.find( ' ' ) ;
// 如果文件名的长度大于 16 那么我们忽略前 16 个字符
if ( fileLength > 16 ) {
errors[index].first = errors[index].first.erase( 0, fileLength - 16 ) ;
}
cout << errors[index].first << ' ' << errors[index].second << endl ;
++ index ;
}
return 0 ;
}


第二次做的时候发现有个小细节
bool compare( pair<string, int> first, pair<string, int> next ) {
return first.second > next.second ;
}
不能写成
bool compare( pair<string, int>& first, pair<string, int>& next ) {
return first.second > next.second ;
}
不能加引用!
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace::std ;

bool compare( pair<string, int> first, pair<string, int> next ) {
return first.second > next.second ;
}

int main() {
string input ;
string file ;
vector<pair<string, int>> errors ;
while ( getline( cin, input ) ) {
if ( input.empty() == true ) break ;
file = input.substr( input.rfind( "\\" ) + 1 ) ;
errors.push_back( make_pair( file, 1 ) ) ;
for ( int i = 0; i < errors.size() - 1; ++ i ) {
if ( file == errors[i].first ) {
++ errors[i].second ;
errors.pop_back() ;
break ;
}
}
}

stable_sort( errors.begin(), errors.end(), compare ) ;

int index = 0 ;
while ( index < 8 && index < errors.size() ) {
int filelength = errors[index].first.find( ' ' ) ;
if ( filelength > 16 ) {
errors[index].first = errors[index].first.erase( 0, filelength - 16 ) ;
}

cout << errors[index].first << " " << errors[index].second << endl ;
++ index ;
}

return 0 ;
}


第三次做:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

using namespace::std ;

bool compare( pair<string, int> first, pair<string, int> next ) {
return first.second > next.second ;
}

int main() {
string input ;
vector<pair<string, int>> vec ;

while ( getline( cin, input ) ) {
if ( input.empty() == true ) break ;
input = input.substr( input.rfind( "\\" ) + 1 ) ;
vec.push_back( make_pair( input, 1 ) ) ;
for ( int i = 0; i < vec.size() - 1; ++ i ) {
if ( input == vec[i].first ) {
++ vec[i].second ;
vec.pop_back() ;
break ;
}
}
}

stable_sort( vec.begin(), vec.end(), compare ) ;

int index = 0 ;
while ( index < 8 && index < vec.size() ) {
int file_length = vec[index].first.find( ' ' ) ;
if ( file_length > 16 ) {
vec[index].first = vec[index].first.erase( 0, file_length - 16 ) ;
}

cout << vec[index].first << " " << vec[index].second << endl ;
++ index ;
}

return 0 ;
}

一开始我忘了给index初始化为0,错误的写法:
int index ;
int main() {
int a,b;
while(scanf("%d %d",&a, &b) != EOF)//注意while处理多个case
printf("%d",a+b); //根据题目情况是否输出回车
return 0;
}

你的代码仅仅处理了一个测试用例,没有循环处理多个测试用例。
比如对于求解A+B的和的题目,需要按照以下代码来处理



正确的写法:
int index = 0 ;

第四次做:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace::std ;

bool cmp( pair<string, int> first, pair<string, int> next ) {
return first.second > next.second ;
}

int main() {
string input ;
vector<pair<string, int>> vec ;

while ( getline( cin, input ) ) {
if ( input.empty() == true ) break ;
input = input.substr( input.rfind( "\\" ) + 1 ) ;
vec.push_back( make_pair( input, 1 ) ) ;
for ( int i = 0; i < vec.size() - 1; ++ i ) {
if ( vec[i].first == input ) {
++ vec[i].second ;
vec.pop_back() ;
break ;
}
}
}

stable_sort( vec.begin(), vec.end(), cmp ) ;

int index = 0 ;
while ( index < 8 && index < vec.size() ) {
int file_length = vec[index].first.find( ' ' ) ;
if ( file_length > 16 ) {
vec[index].first = vec[index].first.substr( file_length - 16 ) ;
}
cout << vec[index].first << " " << vec[index].second << endl ;
++ index ;
}

return 0 ;
}