华为模拟题

来源:互联网 发布:淘宝刷欢乐豆原理 编辑:程序博客网 时间:2024/05/29 07:09

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

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

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

addCandidate xx1

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

vote xx1

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

getVoteResult

#include<iostream>#include<string>#include<map>using namespace std;int main(){map<string,int> vote_cnt; //投票计数string name,temp;int invalid=0;   //无效候选人计数while(cin>>temp){if(temp=="addCandidate"){        //遇到add命令,添加候选人,每人票数为0cin>>name;vote_cnt.insert(pair<string,int>(name,0)); }if(temp=="vote"){                      //投票命令cin>>name;map<string,int>::iterator it=vote_cnt.begin();it=vote_cnt.find(name);if(vote_cnt.count(name)==0)  //如果输入的名字不在候选人中,无效计数+1++invalid;                          //否则该候选人票数+1elseit->second ++;}if(temp=="getVoteResult"){       //遇到getVoteResult命令,输出结果map<string,int>::iterator it;for(it=vote_cnt.begin();it!=vote_cnt.end();++it)cout<<it->first<<" "<<it->second<<endl;cout<<invalid<<endl;}}return 0;}


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

#include <stdio.h>int max_int(const char* s) {    int v = 0, maxv = 0;    int i;    for (i = 0; s[i]; i++) {        v = v * 10 + s[i] - '0';        if (s[i] >= s[i+1]) {            if (v > maxv) {                maxv = v;            }            v = 0;        }    }    return maxv;} int main() {    char s[100];    while (scanf("%s", s) != EOF) {        printf("%d\n", max_int(s));    }    return 0;}


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


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


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

 

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

#include <iostream>   #include <vector>   #include <string>   using namespace std;    const int MAX = 30;  int N, M;  string sGoal;// 待匹配字符串  struct Node  {       int row;       int col;  };    char chess[MAX][MAX];  // 记录单词迷宫bool visit[MAX][MAX];  // visit[i][j] = true表示[i][j]是否已经走过,bool ava[MAX][MAX];// ava[i][j] = true表示[i][j]在单词迷宫内// ava[i][j] = false表示[i][j]已经越界vector<Node> vec;// 储存符合待匹配字符串起始字符的位置  int r[] = {-1, 0, 1, 0};  int c[] = {0, -1, 0, 1};    // row: 迷宫横坐标 col:迷宫纵坐标 index:已经搜索到符合要求的字符串长度 length:待匹配字符串长度bool DFS(int row, int col, int index, int length)  {       if(index == length)// 已经在迷宫内搜索到符合要求的字符串     {            return true;       }         int i;       for(i = 0; i < 4; i++)  // 0~3表示分别代表上,左,下,右四个方向进行搜索     {            int rt = row + r[i];// 根据搜索方向更新坐标          int ct = col + c[i];            if(!visit[rt][ct] && ava[rt][ct] && chess[rt][ct] == sGoal[index])// 判断更新后状态是否满足要求          {                 visit[rt][ct] = true;                 if( DFS( rt, ct, index+1, length) )  // DFS               {                      return true;                 }                 visit[rt][ct] = false;            }       }       return false;  }                  int main()  {       cin >> N >> M;       cin >> sGoal;       int i,j;       memset(visit, false, sizeof(visit));// 初始化为false;     memset(ava, false, sizeof(ava));       for(i = 1; i <= N; i++)// 输入单词迷宫     {            for(j = 1; j <= M; j++)            {                 char cTemp;                 cin >> cTemp;                 chess[i][j] = cTemp;                 ava[i][j] = true;// 单词迷宫内ava[i][j]=true               if(cTemp == sGoal[0])// 将待匹配字符串起始字母作为DFS搜索的起点,节省时间               {                      Node temp;                      temp.row = i;                      temp.col = j;                      vec.push_back(temp);                 }            }       }         bool flag = false;// flag = false表示在单词迷宫内没有搜索到待匹配字符     for(i = 0 ; i < vec.size(); i++)       {            if( DFS(vec[i].row, vec[i].col, 1, sGoal.size()) )            {                 cout << "YES" << endl;                 flag = true;                 break;            }       }         if(!flag)       {            cout << "NO" << endl;       }         return 0;  }  




0 0
原创粉丝点击