2016华为校招上机笔试练习题

1、最高分是多少

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF){
        int *score = (int*)malloc(sizeof(int)*(n+1));
        int res[5000];
        int cnt = 0;
        for(int i=1; i<=n; i++)//这里写成for(int i=0; i<n; i++)可以提交成功,但是那是错误的,估计是华为OJ测试平台有问题,测试平台的下标可能是从0开始的,坑爹
            scanf("%d",&score[i]);
        char t;
        int a=0,b=0;
        while(m--){
            scanf("%c",&t);
            if(t == '
')
                scanf("%c",&t);
            scanf("%d %d",&a,&b);
            if(t == 'Q'){
                int max = 0;
                for(int i=a;i<=b;i++){
                    if(score[i] > max)
                        max = score[i];
                }
                res[cnt++] = max;
            }else
                score[a] = b;
        }
        for(int i=0; i<cnt; i++)
            printf("%d
",res[i]);
        free(score);
    }
    return 0;
}

2、简单错误记录

#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Log{
    string path;
    int row;
    int count;
};
static int log_cnt = 0;
void doLog(Log &lg){ int len = lg.path.size(),cnt = 0; for(int i=len-1; i>=0 && cnt<16; i--){ if(lg.path[i] == '\') break; else{ cnt ++; } } lg.path = lg.path.substr(len-cnt,cnt); } void writeLog(vector<Log> &vec,Log &lg){ doLog(lg); for(int i=0;i<vec.size();i++){ if(vec[i].path == lg.path && vec[i].row == lg.row){ vec[i].count ++; return ; } }

   lg.count = 1;
    vec[log_cnt%8] = lg;
    log_cnt ++;

}


int main(){
    vector<Log> vec;
    Log lg;
    while(cin>>lg.path>>lg.row){
        writeLog(vec,lg);
    }
    for(int i=0; i<vec.size(); i++)
        cout<<vec[i].path<<" "<<vec[i].row<<" "<<vec[i].count<<endl;
    return 0;
}

3、扑克牌大小

#include <iostream>
#include <string>
#include <vector>
using namespace std;

//个子 对子 顺子 三个 四个 对王
static int judgeType(vector<char> &vec,int *value){
    if(vec.size()==2){
        if(vec[0]=='r' || vec[0]=='R') 
            return 6;
        else{
               *value=vec[0];
               return 2;
        }
    }
    *value=vec[0];
    return vec.size();
}

static void print(vector<char> &vec){
    if(vec[0] == 'r')
        cout<<"joker";
    else if(vec[0] == 'R')
        cout<<"JOKER";
    else if(vec[0] == 'T')
            cout<<"10";
    else
        cout<<vec[0];
    for(int i=1; i<vec.size(); i++){
        if(vec[i] == 'r')
            cout<<" joker";
        else if(vec[i] == 'R')
            cout<<" JOKER";
        else if(vec[i] == 'T')
            cout<<" 10";
        else
            cout<<" "<<vec[i];
    }
    cout<<endl;
}
bool comp(char a,char b){
    switch(a){
    case 'T': a = 'a';break;
    case 'J': a = 'b';break;
    case 'Q': a = 'c';break;
    case 'K': a = 'd';break;
    case 'A': a = 'e';break;
    case 'r': a = 'f';break;
    case 'R': a = 'g';break;
    }
    switch(b){
    case 'T': b = 'a';break;
    case 'J': b = 'b';break;
    case 'Q': b = 'c';break;
    case 'K': b = 'd';break;
    case 'A': b = 'e';break;
    case 'r': b = 'f';break;
    case 'R': b = 'g';break;
    }
    return a>b;
}


int main(){
    string str;
    while(getline(cin,str)){
        int len = str.size();
        vector<char> vec1,vec2;
        bool flag = false;
        for(int i=0; i<len; i++){
            if(str[i] == '-'){
                flag = 1;
                continue;
            }
            else if(isspace(str[i]))
                continue;
            if(flag){
                if(str[i]=='j')    {vec1.push_back('r');i=i+4;}
                else if(str[i]=='J' && i==len-1){vec1.push_back('J');}
                else if(str[i]=='J' && str[i+1]=='O'){vec1.push_back('R');i=i+4;}
                else if(str[i]=='1') {vec1.push_back('T');i=i+1;}
                else vec1.push_back(str[i]);
            }else{
                if(str[i]=='j')    {vec2.push_back('r');i=i+4;}
                else if(str[i]=='J' && i==len-1){vec2.push_back('J');}
                else if(str[i]=='J' && str[i+1]=='O'){vec2.push_back('R');i=i+4;}
                else if(str[i]=='1') {vec2.push_back('T');i=i+1;}
                else vec2.push_back(str[i]);
            }
        }
        int value1 = 0,value2 = 0;
        int type1 = judgeType(vec1,&value1);
        int type2 = judgeType(vec2,&value2);
        if(type1 == type2){
            if(comp(value1,value2)) print(vec1);
            else print(vec2);
        }else{
            if((type1==4 || type1==6) && type2<6) print(vec1);
            else if((type2==4 || type2==6) && type1<6) print(vec2);
            else cout<<"ERROR"<<endl;
        }
    }

    return 0;
}

  华为上机题,对算法本身要求并不高,关键是要注意细节。华为OJ平台感觉做的很不好,直接显示错误或正确,一点错误提示都没有。最坑的就是每题只有5次提交机会,搞的大家都不敢轻易提交了。

  版权所有,欢迎转载,转载请注明出处。

原文地址:https://www.cnblogs.com/whc-uestc/p/4733992.html