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

首先,我发现我过程中要比较文件名和行号的组合,不仅如此,还要记录这个组合出现的次数。所以果断采用结构体:

struct Error{
    string filename;
    string line;
    int count;
};
剩下的不难,就是要求多,细节多。

首先读入一个case:while(cin >> name >> num)

去掉name里的路径,只留下完整的文件名,用到了函数name.find_last_of("\\");注意两点,一个是这个函数的使用,另一个是注意转义字符\

得到完整的文件名filename之后遍历结果集,判断是否出现过filename和num的组合,出现的话,该结构体的count加1.否则加入结果集,count为1

然后按照要求准备输出,输出之前要排序。这里用到结构体的自定义的比较函数,从大到小排序:

bool comp(const Error &a, const Error &b)
{
    return a.count > b.count;
}
排序后不要忘记两个输出细节:最多输出8条记录,文件名超过16的,只输出后16个字符。

#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
struct Error{
    string filename;
    string num;
    int count;
};
vector<Error> v;
bool comp(const Error &a, const Error &b){
    return a.count > b.count;
}
void createError(string name, string num){
    int p = name.find_last_of("\\");
    string filename = name.substr(p+1);
    int i = 0, size = v.size();
    for(i = 0; i < size; ++i){
        if(v[i].filename == filename && v[i].num == num){
            v[i].count++;
            break;
        }
    }
    if(i == size){
        Error e;
        e.filename = filename;
        e.num = num;
        e.count = 1;
        v.push_back(e);
    }
}
int main()
{
    string name, num;
    while(cin >> name >> num){
        createError(name, num);
    }
    sort(v.begin(), v.end(), comp);
    int size = min((int)v.size(), 8);
    for(int i = 0; i < size; ++i){
        string nameres;
        if(v[i].filename.size() > 16){
            nameres = v[i].filename.substr(v[i].filename.size()-16);
        }else{
            nameres = v[i].filename;
        }
        cout << nameres << " " << v[i].num << " " << v[i].count << endl;
    }
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值