码迷,mamicode.com
首页 > 其他好文 > 详细

华为机试题目

时间:2015-09-09 11:13:11      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

1.求区间最高分

int getMax(int *pScore, int low, int high)
{
    if(low > high)
    {
        high = low ^ high;
        low = low ^ high;
        high = low ^ high;
    }
    int maxScore = pScore[low];
    for(int i = low + 1; i <= high; ++i)
    {
        if(maxScore < pScore[i])
        {
            maxScore = pScore[i];
        }
    }
 
    return maxScore;
}
 
int main()
{
    int N = 0;
    int M = 0;
    while(cin >> N >> M)
    {
        int *pScore = new int[N + 1];
        for(int i = 1; i <= N; ++i)
        {
            cin >> pScore[i];
        }
 
        for(int i = 0; i < M; ++i)
        {
            char ch = \0;
            int low = 0;
            int high = 0;
 
            cin >> ch >> low >> high;
 
            if(Q == ch)
            {
                cout << getMax(pScore, low, high) << endl;
            }
            else if(U == ch)
            {
                pScore[low] = high;
            }
            else
            {
                // continue;
            }
        }
 
        delete[] pScore;
    }
 
    return 0;
}

 

2.开发一个简单错误记录功能小模块,能够记录出错的代码坐在的文件名称和行号。 

struct info {//记录出现的顺序,和次数
    int rank;
    int count;
    info(int rank, int count) {
        this->rank = rank;
        this->count = count;
    }
};
struct fullinfo {//一条完整的结果,字符串和次数
    string file;
    int rank;
    int count;
    fullinfo(string file, int rank, int count) {
        this->file = file;
        this->rank = rank;
        this->count = count;
    }
};
struct classcomp {//set的比较器
    bool operator()(const struct fullinfo& f1, const struct fullinfo& f2) {
        if (f1.count == f2.count)
            return f1.rank<f2.rank;
        return f1.count>f2.count;
    }
};


typedef struct info INFO;
typedef struct fullinfo FULLINFO;
int main() {
    unordered_map<string, INFO> record;
    unordered_map<string, INFO>::iterator it;
    unordered_map<string, INFO>::const_iterator itfind;
    set<FULLINFO, classcomp> ret;
    set<FULLINFO, classcomp>::iterator sit;
    string linestr;//一行输入
    string file;//文件名+行号
    int pos;//空格的位置
    int i = 1;
    while (getline(cin, linestr)) {
        if (linestr.length() == 0)
            break;
        pos = linestr.rfind("\\");
        file = linestr.substr(pos + 1);//拆分得到最后的filename和count
        itfind = record.find(file);//在map中查看是否已经有了该字符串,没有则插入,有则次数加1
        if (itfind == record.end()) {
            INFO tmpi(i, 1);
            record.insert(pair<string, INFO>(file, tmpi));
        }
        else {
            INFO tmpi(itfind->second.rank, itfind->second.count + 1);
            record.erase(file);
            record.insert(pair<string, INFO>(file, tmpi));
        }
        i++;
    }
    for (it = record.begin();it != record.end();it++) {
        FULLINFO tmpfull(it->first, it->second.rank, it->second.count);//构建排序的set集合
        ret.insert(tmpfull);
    }
    for (i = 0, sit = ret.begin();sit != ret.end() && i<8;++sit, ++i) {//最多输出8条记录,file少于16位
        if (file.find(" ") <= 16) {
            cout << (*sit).file << " " << (*sit).count << endl;
        }
        else {
            cout << (*sit).file.substr(file.find(" ") - 16) << " " << (*sit).count << endl;
        }

    }
    return 0;
}

 

华为机试题目

标签:

原文地址:http://www.cnblogs.com/jason1990/p/4793853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!