华为2019届校招笔试题

1.

给出一个字符串,将重复的字符去除,仅保留第一次出现的字符,且保持去重后的字符在原字符串中的顺序不变。

输入数据是一个字符串(不包含空格)

输出去重后的字符串

输入:12ere2

输出:12er

#include<iostream>
#include<string>
#include<algorithm>
#include<unordered_set>

using namespace std;

int main()
{
	string instr,outstr;
	unordered_set<char>  sc;
	getline(cin,instr);
	for(auto c:instr)
	{
		if(sc.find(c)==sc.end())
		{
			sc.insert(c);
			outstr.append(1,c);
		}
	}
	cout<<outstr<<endl;
   return 0;
}

2

代码如下:

#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

int main()
{
	vector<pair<int,int>> time{{12,13},{13,14},{14,15},{15,16},
		                       {16,17},{17,18},{18,19},{19,20}};
    vector<pair<int,int>> people;
    map<pair<int,int>,int> result;
    int a,b;
    while(true)
    {
		cin>>a;
		if(cin.get()==','){
			cin>>b;
			if(a==-1&&b==-1){
				break;}
			people.push_back(make_pair(a,b));
		}
	}
	for(pair<int,int> pa:people)
	{
		for(pair<int,int> t:time)
		{
			if(pa.first<=t.first&&pa.second>=t.second)
			{
				++result[t];
			}
	    }
	}
	for(pair<pair<int,int>,int> re:result)
	{
	  cout<<'['<<re.first.first<<','<<re.first.second<<')'<<':'<<re.second<<endl;
	}
	return 0;
}

3.

 

 

 代码如下:

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

int main()
{
   stack<int> num;
   stack<char> ops;
   string str;
   getline(cin,str);
   int result;
   int i=0;
   for(;i<str.size();)
   {
	   int t=0;
	   while(str[i]==' ') {
		   ++i;
		   ++t;
	   }
	   if(t>1){  //' ' is more
		   result=-1;
		   break;}
	   if(str[i]!='.'&&str[i]<'0'||str[i]>'9') {
		   if(str[i]==')'){
			   char op=ops.top();
			   ops.pop();
			   if(op=='^'){
				   int re=num.top();
				   num.pop();
				   num.push(re+1);
			   }else if(op=='+'||op=='*'){
				   int a=num.top();
				   num.pop();
				   int b=num.top();
				   num.pop();
				   if(op=='+'){
					   num.push(a+b);
				    }else{
						num.push(a*b);}
			   }else // other op{
				   result=-1;
				   break;
			   }
			   ops.pop(); //delete'('
			   if(ops.empty()){  //the tree is end
				   result=num.top();
				   break;
				   }
		   }else{
			   ops.push(str[i]);
		   }
		   ++i;
	   }else if(str[i]!='.'){
		   t=i;
		   while(str[i]>='0'&&str[i]<='9'){
			   ++i;
		   }
		   num.push(stoi(str.substr(t,i-t)));
	   }else{  //'.'
		   break;
		   result=-1;
		   }
   }
   if(i==str.size()){
		  result=-1;
	}
	   cout<<result<<endl;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值