华为笔试刷题

1、查成绩

老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩.
我的解法:

#include<iostream>
#include<vector>
#include<algorithm>
 
using namespace std;
int main(){
    //N表示学生的数目,M表示操作的数目
    int N,M;
    while(cin >>N >>M){
        //接着输入初始成绩
        vector<int> grades;
        for(int i = 0;i < N;i++){
            int grade;
            cin>>grade;
            grades.push_back(grade);
        }
        //再读取M行的操作数据
        for(int i = 0;i < M;i++){
            string flag ;
            int A,B;
            cin>>flag>>A>>B;
            if(flag == "Q"){
                if(A > B){
                    swap(A,B);
                }
                //获取AB间的最高成绩
                int maxgrade=-1;
                for(int j = A-1;j < B;j++){
                    //maxgrade = max(grades[j],maxgrade);
                    if(grades[j] > maxgrade){
                        maxgrade = grades[j];
                        //cout<<"..."<<endl;
                    }
                }
                cout<<maxgrade<<endl;
            }else if(flag == "U"){
                //将ID为A的同学更新为成绩B
                grades[A-1] = B;
            }else{
             
            }
        }
    }
    return 0;
}

高手的解法,直接使用了max_element():

#include<algorithm>
#include<vector>
using namespace std;
  
int main()
{
    int N,M;
    while(cin>>N>>M){
        vector<int> stu(N);
        int a,b;char c;
        for(int i=0;i<N;++i)cin>>stu[i];
        for(int j=0;j<M;++j){
            cin>>c>>a>>b;
            if(c=='Q'){
                if(a>b)swap(a,b);
                cout<<*max_element(stu.begin()+a-1,stu.begin()+b)<<endl;                   
            }   
            if(c=='U')stu[a-1]=b;        
        }
    }
    return 0;
}

2、记录错误

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

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
bool comp(const pair<string,int> &one, const pair<string,int> &other){
    return one.second > other.second;
}
 
int main(){
    string input;
    vector<pair<string,int>> result;
    while(getline(cin,input)){
        //取出文件名字和行号
        int pos = input.rfind("\\");
        string fileAndLine = input.substr(pos+1);
        bool isExist = false;
        for(int i = 0; i< result.size();i++){
            if(result[i].first == fileAndLine){
                result[i].second++;
                isExist = true;
                break;
            }
        }
        if(!isExist){
            pair<string,int> ele;
            ele = make_pair(fileAndLine,1);
            result.push_back(ele);
        }
    }
    //排序
    sort(result.begin(),result.end(),comp);
    //打印(超过八行只是打印前面的八行)
    for(int i = 0; i < result.size() && i < 8;i++){
        int pos = result[i].first.find(" ");
        string fileName = result[i].first.substr(0,pos);
        if(fileName.size() > 16){
            result[i].first.erase(0,fileName.size()-16);
        }
        cout<<result[i].first<<" "<<result[i].second<<endl;
    }
    return 0;
}

3、比较扑克牌

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//枚举所有的牌大小
enum Card{
    THREE=0,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,J,Q,K,A,TWO ,joker,JOKER
};
const vector<string> allCards = {"3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"};
vector<Card> spiltHand(string handcards){
    //cout<<handcards<<endl;
    vector<Card> result;
    handcards += " ";
    size_t  pos = handcards.find(" ");
    size_t  size = handcards.size();
    while(pos  != string::npos){
        //取出片段,移除已经使用过的值
        string cut = handcards.substr(0,pos);
        //handcards.erase(0,pos);
        handcards = handcards.substr(pos+1);
        //给每张牌分类
        for(int i = 0; i < allCards.size();i++){
            if(allCards[i] == cut){
                Card card ;
                card = Card(i);
                result.push_back(card);
                break;
            }
        }
        pos = handcards.find(" ");
    }
    return result;
}
void printMesg(vector<Card > &mesg){
    for(auto ele: mesg){
        cout<<allCards[ele]<<" ";
    }
    cout<<endl;
}
int main(){
    string input;
    while(getline(cin,input)){
        if(input.size() == 0){
            break;
        }
        //得到两副手牌
        int pos = input.find("-");
        string firstHand = input.substr(0,pos);
        string secondHand = input.substr(pos +1);
        vector<Card > FirstHand = spiltHand(firstHand);
        vector<Card > SecondHand = spiltHand(secondHand);
        //printMesg(FirstHand);
        //printMesg(SecondHand);
        if(FirstHand.size() != SecondHand.size()){
            //牌数不相等,要么是有对王,或者炸弹,要么是ERROR
            if((FirstHand[0] == Card ::joker && FirstHand[1] == JOKER )||(SecondHand[0] == Card ::joker && SecondHand[1] == JOKER)){
                cout<<"joker JOKER"<<endl;
            } else if(FirstHand.size() == 4 ) {
                printMesg(FirstHand);
            } else if(SecondHand.size() == 4){
                printMesg(SecondHand);
            }else{
                cout<<"ERROR"<<endl;
            }
        }else{
            //剩下的正常比大小
            for(int i = 0; i<FirstHand.size();i++){
                if(FirstHand[i] > SecondHand[i]){
                    printMesg(FirstHand);
                    break;
                } else if(FirstHand[i]<SecondHand[i]){
                    printMesg(SecondHand);
                    break;
                }
            }
        }
    }
    return 0;
}

高手的解:

//“输入每手牌可能是个子,对子,顺子(连续5张),三个,炸弹(四个)和对王中的一种,
//不存在其他情况,由输入保证两手牌都是合法的,顺子已经从小到大排列“
//这句规则让牌面类型的确定和大小的比较直接可以转换为牌个数的比较了,这是解决测试问题的捷径!
//所以一定要善于利用题目已知条件!!!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    string line;
    while(getline(cin,line)){
        if(line.find("joker JOKER")!=-1)
            cout<<"joker JOKER"<<endl;
        else{
            int dash=line.find('-');
            string car1=line.substr(0,dash);
            string car2=line.substr(dash+1);
            int c1=count(car1.begin(),car1.end(),' ');
            int c2=count(car2.begin(),car2.end(),' ');
            string first1=car1.substr(0,car1.find(' '));
            string first2=car2.substr(0,car2.find(' '));
            string str="345678910JQKA2jokerJOKER";
            if(c1==c2){
                if(str.find(first1)>str.find(first2))
                    cout<<car1<<endl;
                else
                    cout<<car2<<endl;
            }else
                if(c1==3)
                    cout<<car1<<endl;
                else if(c2==3)
                    cout<<car2<<endl;
                else
                    cout<<"ERROR"<<endl;
        }
    }
}

nkn

#include<iostream>
using namespace std;
struct myList{
    int value;
    myList * next;
    myList(){
        value = -1;
        next = nullptr;
    }
};
int main(){
    int num;
    while(cin >> num){
        if(num == 0){
            break;
        }
        //初始化一个游标和头指针
        myList *p,*first;
        for(int i = 0; i < num;i++){
            myList *next;
            next->value = i;
            p->next = next;
            if(i == 0){
                first = next;
            }
            p = next;
        }
        //最后把尾部的指针指向开头
        p->next = first;
        p = first;
        //当p的下一个元素为NULL时,跳出
        while(p->next != NULL){
            myList *front = p->next;
            myList *back = p->next->next->next;
            delete front->next;
            front->next = back;
            p = back;
        }
        cout<<p->value<<endl;
    }
}

4、删数

有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。

#include<iostream>
using namespace std;
struct myList{
    int value;
    myList * next;
};
int main(){
    int num;
    while(cin >> num){
        if(num == 0){
            break;
        }
        //初始化一个游标和头指针
        myList *p,*first;
        first = new myList;
        first->value = 0;
        first->next = NULL;
        p = first;
        for(int i = 1; i < num;i++){
            //cout<<i<<endl;
            myList *next = new myList;
            next->value = i;
            next->next = NULL;
            p->next = next;
            p = next;
        }
        //最后把尾部的指针指向开头
        p->next = first;
        p = first;
        //cout<<p->value<<endl;
        //当p的下一个元素为NULL时,跳出
        while(p->next != p){
            myList *target = p->next->next;
            p->next->next = target->next;
            p = target->next;
            delete target;
        }
        cout<<p->value<<endl;
        //delete first;
        delete p;
    }
}

更好的解法,使用队列:

用队列模拟,队首取数,用一个计数器计数,隔2个删一个,其他的重新放到队尾 #include<iostream>
#include<queue>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        queue<int> q;
        for(int i=0;i<n;i++)
        {
            q.push(i);
        }
        int count=0;
        while(q.size()!=1)
        {
            if(count!=2)
            {
                int b=q.front();
                q.pop();
                q.push(b);
                count++;
            }
            else
            {
                q.pop();
                count=0;
            }
        }
        int c=q.front();
        cout<<c<<endl;
    }
    return 0;
}

5、字符串集合

输入一个字符串,求出该字符串包含的字符集合

#include<iostream>
using namespace std;
int main(){
    //按行读取数据
    string input;
    while(getline(cin, input)){
        //遍历字符串的每一个字符
        string output = "";
        for(int i = 0; i < input.size();i++){
            size_t pos = output.find(input[i]);\
            //cout<<pos<<endl;
            if(pos == string::npos){
                //没在前面的结果中找到
                output +=input[i];
            }
        }
        cout<<output<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值