【2019秋招刷题】华为0911笔试题

  • 自动驾驶,发送消息模拟
  • 代码如下:
#include 
#include 
#include 
using namespace std;
string charToStr(char chr)
{
	char chr_str[2] = {chr,'\0'};
	string str = chr_str;
	return str;
}

int strToInt(string str)
{
	char cstr[10];
	strcpy(cstr, str.c_str());
	int iii = atoi(cstr); 
	return iii;
}
//这里start是{所在位置,end是}所在位置
vector<int> strToIntArr(string str, int start, int end)
{
	vector<int> vec;
	string str_int;
	for(int counter = start+1; counter < end;counter++)
	{
		if(str[counter] == ',')
		{
			int tmp = strToInt(str_int);
			vec.push_back(tmp);
			str_int = "";
		}
		else if(counter == end -1)
		{
			str_int += charToStr(str[counter]);
			int tmp = strToInt(str_int);
			vec.push_back(tmp);
			str_int = "";

		}
		else
		{
			str_int += charToStr(str[counter]);
		}
	}
	return vec;
}
int main()
{
	string str_in;
	vector<int> vec_a;
	vector<int> vec_b;
	int r;
	int start, end;
	char arr_label;
	
	cin>>str_in;
	for(int counter = 0; counter < str_in.length();counter++)
	{	
		if(str_in[counter] == 'R')
		{
			r = strToInt(str_in.substr(counter + 2));
			break;
		}
		else if(str_in[counter] == '=')
		{
			arr_label = str_in[counter - 1];
		}
		else if(str_in[counter] == '{')
			start = counter;
		else if(str_in[counter] == '}')
		{
			end = counter;
			if(arr_label == 'A')
			{
				vec_a = strToIntArr(str_in,start,end); 
			}
			else
			{
				vec_b = strToIntArr(str_in,start,end);
			}
		}
	}

	vector<int> res_first;
	vector<int> res_second;
	int begin_pos_b = 0;
	int numOfpairs = 0;
	for(int counter1 = 0; counter1 < vec_a.size();counter1++)
	{
		numOfpairs = 0;
		for(int counter2 = begin_pos_b; counter2 < vec_b.size();counter2++)
		{
			if(vec_b[counter2] < vec_a[counter1])
			{
				begin_pos_b = counter2;
				continue;
			}

			if(vec_b[counter2] - vec_a[counter1] <= r)
			{
				res_first.push_back(vec_a[counter1]);
				res_second.push_back(vec_b[counter2]);
				numOfpairs++;
				continue;
			}
			if(numOfpairs == 0)
			{
				res_first.push_back(vec_a[counter1]);
				res_second.push_back(vec_b[counter2]);
			}
			break;
		}
	}

	for(int counter = 0; counter < res_second.size();counter++)
	{
		cout<<"("<<res_first[counter]<<","<<res_second[counter]<<")";
	}
	cout<<endl;
	return 0;
}

  • 字符串反转,代码如下;
#include 
#include 
#include 
using namespace std;
bool isValid(char chr)
{
	if(chr >= 'a' && chr <= 'z')
		return true;
	if(chr >= 'A' && chr <= 'Z')
		return true;
	if(chr >= '0' && chr <= '9')
		return true;
	return false;
}
string charToStr(char chr)
{
	char chr_str[2] = {chr,'\0'};
	string str = chr_str;
	return str;
}

int main()
{
	string str_in;
	getline(cin,str_in);

	string str_commu = "";
	stack<string> stk_str;
	for(int counter = 0; counter < str_in.length(); counter++)
	{
		if(isValid(str_in[counter]))
		{
			str_commu += charToStr(str_in[counter]);
			if(counter == str_in.length() -1)
			{
				stk_str.push(str_commu);
			}
		}
		else
		{
			if(str_in[counter] == '-')//如果是连续两个--的情况要一次性处理完毕
			{
				if(counter == str_in.length() -1)
				{
					if(str_commu != "")
					{
						stk_str.push(str_commu);
					}
					break;
				}
				else if(counter > 0 && isValid(str_in[counter - 1])&&isValid(str_in[counter + 1]))
				{
					str_commu += charToStr(str_in[counter]);
				}
				else if(str_in[counter + 1] == '-')
				{
					if(str_commu != "")
						stk_str.push(str_commu);
					str_commu = "";
					counter ++;
					continue;
				}
			}
			else if(str_commu != "")
			{
				stk_str.push(str_commu);
				str_commu = "";
			}
		}
	}
	while(stk_str.size() != 0)
	{
		cout<<stk_str.top();
		stk_str.pop();
		if(stk_str.size() != 0)
		{
			cout<<" ";
		}
	}
	cout<<endl;
	return 0;	
}

  • 航班改签,下回更新。。。

你可能感兴趣的:(秋招刷题)