欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

华为2017校招C++岗笔试题

程序员文章站 2022-07-28 20:08:44
1.删除字符串中的指定字符 1.1问题描述 输入两个字符串M和N,从字符串M中删除字符串N中所有的字符。例如,输入”abcda”和”ac&r...

1.删除字符串中的指定字符

1.1问题描述

输入两个字符串M和N,从字符串M中删除字符串N中所有的字符。例如,输入”abcda”和”ac”,则删除之后的第一个字符串变成”bd”。

1.2问题求解

这个比较简单,给出如下参考代码:

#include 
using namespace std;

void deleteCharacter(string& str0,string& str1){
    for(int i=0;i2.成绩排名2.1问题描述

题目总共包含如下两种格式的字符串命令: LOD GRADE命令,其格式: LOD GRADE:NAME=XiaoMing,MATH=80,LANG=90; (1) 此命令用于导入学生成绩 (2) NAME字段表示学生姓名 (3) MATH字段表示学生数学成绩 (4) LANG字段表示语文成绩 (5) MATH字段和LANG字段顺序不一定MATH在前,LANG在后 (6) 相同的分数,名次相同,后面的名次空缺;例如100,99,99,99,98,98,名次:1,2,2,2,5,5 (7) 此命令会连续执行,直到遇到第一个LST GRADE

LST GRADE命令,其格式: LST GRADE:NAME=XiaoMing; (1) 此命令用于查询学生成绩 (2) NAME字段表示学生姓名 (3) 查询结果格式:姓名 数学 语文 总分 数学排名 语文排名 总排名 (4) 每组用例,此命令仅执行一次

输入: 连续多组LOD GRADE后跟一个LST GRADE查询命令 输出: 输出查询格式为: 姓名 数学 语文 总分 数学排名 语文排名 总排名 样例输入: LOD GRADE:NAME=XiaoMing,MATH=80,LANG=90; LOD GRADE:NAME=XiaoHong,LANG=60,MATH=100; LOD GRADE:NAME=XiaoMei,MATH=70,LANG=90; LST GRADE:NAME=XiaoHong; 样例输出: XiaoHong 100 60 160 1 3 2

2.2问题求解

此问题也不难,没有涉及到复杂的算法,就是比较繁琐,主要考察数据的表示,字符串的提取与排序,下面给出参考代码:

#include 
#include 
#include 
#include 
using namespace std;

struct Student{
    string name;
    int math;
    int lang;
    Student(){
        this->name="";
        this->math=0;
        this->lang=0;
    }
    Student(string name,int math,int lang){
        this->name=name;
        this->math=math;
        this->lang=lang;
    }
    bool operator==(const Student& ele){
        return this->name==ele.name;
    }
};

//自定义比较函数,数学排名
bool compareMath(const Student& left,const Student& right){
    return left.math>right.math; //降序排列
}

//自定义比较函数,语文排名
bool compareLang(const Student& left,const Student& right){
    return left.lang>right.lang; //降序排列
}

//自定义比较函数,总分排名
bool compareTotal(const Student& left,const Student& right){
    return left.math+left.lang>right.math+right.lang; //降序排列
}

int main(){
    vector studentVec;
    string input;
    Student student;
    vector splitedRes;
    while(getline(cin,input)){
        int s=input.find("NAME=");
        int e=input.find(',',s);
        if(input.find("LOD GRADE")!=string::npos){  //输入成绩
            student.name=input.substr(s+5,e-s-5);

            s=input.find("MATH=");
            e=input.find(',',s);
            if(e==string::npos) e=input.length()-1;
            student.math=stoi(input.substr(s+5,e-s-5));

            s=input.find("LANG=");
            e=input.find(',',s);
            if(e==string::npos) e=input.length()-1;
            student.lang=stoi(input.substr(s+5,e-s-5));
            studentVec.push_back(student);
        }else {                                     //查询成绩
            e=input.length()-1;
            string name=input.substr(s+5,e-s-5);
            Student student;
            //数学排名
            std::sort(studentVec.begin(),studentVec.end(),compareMath);
            vector::iterator it=find(studentVec.begin(),studentVec.end(),Student(name,0,0));
            student=*it;
            while(it!=studentVec.begin()&&(it-1)->math==it->math) --it;
            int mathRank=it-studentVec.begin()+1;

            //语文排名
            std::sort(studentVec.begin(),studentVec.end(),compareLang);
            it=find(studentVec.begin(),studentVec.end(),Student(name,0,0));
            while(it!=studentVec.begin()&&(it-1)->lang==it->lang) --it;
            int langRank=it-studentVec.begin()+1;

            //总分排名
            std::sort(studentVec.begin(),studentVec.end(),compareTotal);
            it=find(studentVec.begin(),studentVec.end(),Student(name,0,0));
            while(it!=studentVec.begin()&&(it-1)->math+(it-1)->lang==it->math+it->lang) --it;
            int totalRank=it-studentVec.begin()+1;
            cout<
();){>