2015华为校园招聘机试模拟题及解答

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

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

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

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

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

void main()
{
	std::string commond;
	std::string add = "addCandidate";
	std::string vote = "vote";
	std::string getResult = "getVoteResult";
	std::cin>>commond;
	std::map<std::string,int> voteResult;
	std::vector<std::string> candidate;
	int useless = 0;
	while(commond != getResult)
	{
		if(commond == add)
		{
			std::cin>>commond;
			voteResult.insert(std::make_pair(commond,0));
			candidate.push_back(commond);
		}
		if(commond == vote)
		{
			std::cin>>commond;
			if(voteResult.count(commond) != 0)
				voteResult[commond]++;
			else
				useless++;
		}
		std::cin>>commond;
	}
	std::vector<std::string>::iterator iter = candidate.begin();
	while(iter != candidate.end())
	{
		std::string can = *iter;
		std::cout<<can<<" "<<voteResult.find(can)->second<<std::endl;
		iter++;
	}
	std::cout<<useless<<std::endl;
	//system("pause");
}
2、描述:输入一串数字,找到其中包含的最大递增数。递增数是指相邻的数位从小到大排列的数字。如: 2895345323,递增数有:289,345,23, 那么最大的递减数为345。运行时间限制:无限制内存限制:无限制输入:输入一串数字,默认这串数字是正确的,即里面不含有字符/空格等情况输出:输出最大递增数样例输入:123526897215样例输出:2689
bool isLarger(std::string first,std::string second)
{
	int firstLen = first.length();
	int secondLen = second.length();

	if(firstLen != secondLen)
		return firstLen > secondLen;
	else
	{
		int i = 0;
		while(i < firstLen && first.at(i) == second.at(i))
			++i;
		if(i < firstLen)
			return first.at(i) > second.at(i);
		else
			return false;
	}
}
void main()
{
	std::string number;
	std::cin>>number;
	int length = number.length();
	std::string maxNum = "0";
	int start = 0;
	int end = 0;
	for(int i = 1; i < length;++i)
	{
		while(i < length && number.at(i) > number.at(i -1))
			i++;

		end = i - 1;
		std::string subStr = number.substr(start,end - start + 1);
		if(isLarger(subStr,maxNum))
		{
			maxNum = subStr;
		}
		start = i;
		end = i;
	}
	std::cout<<maxNum<<std::endl;
	//system("pause");
}
在n*m字符迷宫中寻找指定单词,单词需在迷宫中连续出现,存在返回YES,否则返回NO,
bool foundWord(char* matrix,int n,int m,int i, int j,std::string words,int k)
{
	if(k == words.length())
		return true;
	if(i - 1 > -1)
	{
		if(*(matrix + (i-1) * m + j) == words.at(k))
		{
			return foundWord(matrix,n,m,i - 1,j,words,k + 1);
		}
	}
	if(i + 1 < n)
	{
		if(*(matrix + (i+1) * m + j) == words.at(k))
		{
			return foundWord(matrix,n,m,i + 1,j,words,k + 1);
		}
	}
	if(j - 1 > -1)
	{
		if(*(matrix + i * m + j - 1) == words.at(k))
		{
			return foundWord(matrix,n,m,i,j - 1 ,words,k + 1);
		}
	}
	if(j + 1 < m)
	{
		if(*(matrix + i * m + j + 1) == words.at(k))
		{
			return foundWord(matrix,n,m,i,j + 1,words,k + 1);
		}
	}
	return false;
}
void main()
{
	int n = 0;
	int m = 0;
	std::string words;
	int length = words.length();
	std::cin>>n>>m;
	std::cin>>words;
	char *matrix = new char[n * m];
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			std::cin>>*(matrix + i * m + j);
		}
	}
	int i = 0;
	int j = 0;
	bool found = false;

	for(i = 0;!found && i < n; ++i)
	{
		for(j = 0; !found && j < m; ++j)
		{
			if(*(matrix + i * m + j) == words.at(0))
			{
				found = true;
			}
		}
	}
	if(!found)
	{
		std::cout<<"NO";
		return;
	}
	if(foundWord(matrix,n,m,i,j,words,1))
		std::cout<<"YES";
	else
		std::cout<<"NO";

	system("pause");
}

上一个代码中存在一个明显的问题,就是假设首个字母出现多次的情形下,无法正常判断

typedef struct _position
{
	int col;
	int row;
	_position()
	{
		col = -1;
		row = -1;
	}
	_position(int r,int c)
	{
		row = r;
		col = c;
	}

}Position,*PositionPtr;

void main()
{
	//新的思路,记录单词字母对应的矩阵下标,判断其是否连成链
	//关键问题,如何进行存储,
	//由于一个字符可能对应多个下标,选择使用multimap进行存储<key,value1><key,value2>
	int n = 0;
	int m = 0;
	std::string words;
	int length = words.length();
	std::cin>>n>>m;
	std::cin>>words;
	std::multimap<char,Position> record;
	typedef std::multimap<char,Position>::size_type map_size;
	for(int i = 0; i < words.length(); i++)//记录所有字母
	{
		record.insert(std::make_pair(words.at(i),Position(-1,-1)));
	}

	char *matrix = new char[n * m];
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			std::cin>>*(matrix + i * m + j);
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			map_size entries = record.count(*(matrix + i * m + j));//字符在multimap中的个数
			if(entries != 0)
			{
				record.insert(std::make_pair(*(matrix + i * m + j),Position(i,j)));//记录单词字母在矩阵中的位置
			}
		}
	}
	//记录的位置<-1,-1>,<真实位置>
	//对记录进行查找,并判断是否连续,如何进行判断
	//使用栈操作,将多个可行节点入栈
	std::pair<int,Position> stack[10];//int 是word中下标, Positiono下标字母对应坐标
	int top = 0;//指向栈顶
	map_size count = record.count(words.at(0));
	std::multimap<char,Position>::iterator iter = record.find(words.at(0));
	iter++;//首个为<-1,-1>
	for(map_size cnt = 1; cnt != count; cnt++,iter++)
	{
		int col = iter->second.col;
		int row = iter->second.row;
		stack[top++] = std::make_pair(0,Position(row,col));
	}

	while(top != 0)//堆栈操作
	{
		std::pair<int,Position> cur = stack[--top];//栈顶元素输出
		int index = cur.first;
		if(index == words.length() - 1)//表示当前栈元素已经遍历到单词的尾端
		{
			std::cout<<"YES"<<std::endl;
			system("pause");
			return;
		}
		Position cur_pos = cur.second;

		map_size count = record.count(words.at(index + 1));
		std::multimap<char,Position>::iterator iter = record.find(words.at(index + 1));
		iter++;
		for(map_size cnt = 1; cnt != count; cnt++,iter++)
		{
			int col = iter->second.col;
			int row = iter->second.row;
			if(std::abs(col - cur_pos.col) + std::abs(row - cur_pos.row) == 1)//当前单词与上一单词邻近
				stack[top++] = std::make_pair(index + 1,Position(row,col));
		}
	}
	std::cout<<"NO"<<std::endl;
	system("pause");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值