华为软件类校招 2014年9月3日 熟悉机考环境 1.记票统计 2.求最大递增数 3.Word Maze(单词迷宫)

来源:互联网 发布:微信商城端口什么意思 编辑:程序博客网 时间:2024/06/06 13:14

(样题,已对外公布)记票统计
描述: 模拟n个人参加选举的过程,并输出选举结果:假设候选人有四人,分别用“A”、”B”、”C”、”D”表示,选举时开始计票, 若输入的不是“A”、”B”、”C”、”D”则视为无效票。选举结束后获取各个候选人的得票数以及无效票的个数,输出结果以添加候选人的顺序进行顺序输出,最后一行为无效的数量。同时getVoteResult命令为结束命令。

 
运行时间限制: 无限制
内存限制: 无限制
输入: 输入为多行形式输入,每一行为一条命令。输入的命令只会是有效命令不会有非法命令,但可能存在非法的投票,上面已经描述了。

添加候选人的命令如下:addCandidate为命令 xx1为候选人

addCandidate xx1

投票的命令如下:vote为命令 xx1为候选人的字符串

vote xx1

统计投票的命令如下:getVoteResult为命令

getVoteResult
 
输出: 输出结果以添加候选人的顺序进行顺序输出,最后一行为无效的数量。
 
样例输入: addCandidate xx1
addCandidate xx2
addCandidate xx3
addCandidate xx4
addCandidate xx5
addCandidate xx6
vote xx1
vote xx3
vote xx4
vote xx1
vote xx2
vote xx7
vote xx4
vote xx5
vote xx3
vote xx2
vote xx1
vote xx7
getVoteResult
样例输出: xx1 3
xx2 2
xx3 2
xx4 2
xx5 1
xx6 0
2
答案提示:  

 

 

(样题,已对外公开)求最大递增数
描述: 输入一串数字,找到其中包含的最大递增数。递增数是指相邻的数位从小到大排列的数字。如: 2895345323,递增数有:289,345,23, 那么最大的递增数为345。
 
运行时间限制: 无限制
内存限制: 无限制
输入: 输入一串数字,默认这串数字是正确的,即里面不含有字符/空格等情况
 
输出: 输出最大递增数
 
样例输入: 123526897215
样例输出: 2689
答案提示:  

 

 

(样题,已对外公布)Word Maze(单词迷宫)


描述: Word Maze 是一个网络小游戏,你需要找到以字母标注的食物,但要求以给定单词字母的顺序吃掉。如上图,假设给定单词if,你必须先吃掉i然后才能吃掉f。

但现在你的任务可没有这么简单,你现在处于一个迷宫Maze(n×m的矩阵)当中,里面到处都是以字母标注的食物,但你只能吃掉能连成给定单词W的食物。

如下图,指定W为“SOLO”,则在地图中红色标注了单词“SOLO”。

注意区分英文字母大小写,你只能上下左右行走。


运行时间限制: 无限制
内存限制: 无限制

输入: 输入第一行包含两个整数n、m(0<n, m<21)分别表示n行m列的矩阵,第二行是长度不超过100的单词W,从第3行到底n+3行是只包含大小写英文字母的长度为m的字符串。

输出:如果能在地图中连成给定的单词,则输出“YES”,否则输出“NO”。注意:每个字母只能用一次。

样例输入:
5 5
SOLO
CPUCY
EKLQH
CRSOL
EKLQO
PGRBC

样例输出:
YES
答案提示: 

 

 

 

 

以下给出个人的三道题解。第三题机试时没有过,但是不知何因,后面又修改了下,如若还有漏洞bug,欢迎指出!

题1解:

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<vector>#include<list>#include<stack>#include<queue>#include<string>#include<algorithm>#include<utility>#include<hash_map>using namespace std;using namespace stdext;int main(){#if 1#ifndef ONLINE_JUDGE    freopen("testCase-1.txt","r",stdin);#endif#endif    vector<string> candidates;  //依次输入的候选人姓名    vector<int>    nums;        //对应上面每个候选人相应的票数    hash_map<string,int>  strToIntHash; //每个候选人的编号,按添加顺序addCandidate编号    int numOfCandidates = 0;    //总候选人数    int numOfInvalid = 0;       //无效票数    string str,name;    int index=0;    cin>>str;    while(str !=string("getVoteResult")){        if(str==string("addCandidate")){            cin>>name;            candidates.push_back(name);            nums.push_back(0);            strToIntHash[name] = numOfCandidates++;        }        else if(str==string("vote")){            cin>>name;            index = strToIntHash[name];            if(index==0 && name!=candidates[0])                numOfInvalid++;            else{                nums[index]++;            }        }        cin>>str;    }    for(int i=0;i<numOfCandidates;i++)    {        cout<<candidates[i]<<' '<<nums[i]<<endl;    }    cout<<numOfInvalid<<endl;    fclose(stdin);    return 0;}


 

题2解:

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<vector>#include<list>#include<stack>#include<queue>#include<string>#include<algorithm>#include<utility>#include<set>using namespace std;int main(){#if 1#ifndef ONLINE_JUDGE    freopen("testCase-2.txt","r",stdin);#endif#endif    int maxValue = 0;    char buf[10000];    char charTmp[20];    char *begin=NULL,*end=NULL;    int intTmp;    while(scanf("%s",buf)!=EOF){        maxValue = 0;        begin = buf;        for(int i=0; buf[i]!='\0' ;i++){            if(buf[i+1]!='\0' && buf[i+1]>buf[i]){                continue;                                }            else{   // 包括最后的 '\0' ,                end = buf+ i;                strncpy(charTmp,begin,end-begin+1);                charTmp[end-begin+1]='\0';                sscanf(charTmp,"%d",&intTmp);                if(intTmp>maxValue)                    maxValue = intTmp;                //从下一字符 重新开始                begin = buf+i+1;            }        }        printf("%d\n",maxValue);            }    return 0;}


 

题3解(可能有bug存在)

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<vector>#include<list>#include<stack>#include<queue>#include<string>#include<algorithm>#include<utility>using namespace std;//#define DEBUGchar matrix[25][25];bool flag[25][25];char  w[300];int   len_w;int   n,m;bool g_found =false;struct elem{    int x;    int y;    char data;    elem(int x,int y,char data): x(x),y(y),data(data){}};#ifdef DEBUGstack<elem> elemStack;#endifbool search(int i,int j,int index){    //bool result = false;    if( !flag[i][j] || matrix[i][j] != w[index])        return false;    flag[i][j] = false;  //标记为已用 不可用    if(index==(len_w-1)){      //全部找到        goto FoundProcess;    }        if((j-1)>=0 &&flag[i][j-1]&& matrix[i][j-1]==w[index+1])  // left        if(search(i,j-1,index+1)){            goto FoundProcess;        }    if((j+1)<m &&flag[i][j+1]&& matrix[i][j+1]==w[index+1])  // right        if(search(i,j+1,index+1)){            goto FoundProcess;        }    if((i-1)>=0 &&flag[i-1][j]&& matrix[i-1][j]==w[index+1]) // up        if(search(i-1,j,index+1)){            goto FoundProcess;        }    if((i+1)<n &&flag[i+1][j]&& matrix[i+1][j]==w[index+1])  // down        if(search(i+1,j,index+1)){            goto FoundProcess;        }NotFoundProcess:    flag[i][j] = true;    //标记为未用 可用      return false;FoundProcess:#ifdef DEBUG    elemStack.push(elem(i,j,matrix[i][j]));#endif    return true;}int main(){#ifndef ONLINE_JUDGE    freopen("testCase-3.txt","r",stdin);#endif   while(scanf("%d %d",&n,&m)!=EOF){       g_found = false;       for(int i=0;i<25;i++)           for(int j=0;j<25;j++)               flag[i][j] = true;       scanf("%s",w);       len_w = strlen(w);       for(int i=0;i<n;i++)           scanf("%s",matrix[i]);       for(int i=0;i<n;i++){           for(int j=0;j<m;j++){               if(matrix[i][j]==w[0]){                                      if(search(i,j,0)){                       g_found = true;                       goto end;                   }               }           }       }end: if(g_found==true){         printf("YES\n");#ifdef DEBUG         while(elemStack.size()){             elem tmp = elemStack.top();             elemStack.pop();             //cout<<'('<<tmp.x<<','<<tmp.y<<')'<<' '<<tmp.data<<endl;             printf("(%d,%d) %c\n",tmp.x,tmp.y,tmp.data);         }#endif     }     else         printf("NO\n");    }        return 0;}


 

 

0 0
原创粉丝点击