华为笔试软件岗位新鲜出炉

华为笔试第一题AC, 第二题感觉没问题但是通过一直为0, 第三题AC,第三题手撸快170行了,C++代码如下:
第一题:
#include<iostream>
using namespace std;
int main() {
	int arr[100];
	int length = 0;
	while(1) {
		cin>>arr[length++];
		if (getchar() == '\n') break;
	}
	int min_steps = 2147483647;
	for (int i = 1; i < length / 2; i++) {
		int steps = 1;
		int pos = i;
		while(1) {
			int next_steps_num = arr[pos];
			if ((pos + next_steps_num) == (length - 1)) {
				steps++;
				if (steps < min_steps) min_steps = steps;
				break;
			} else if ((pos + next_steps_num) > (length - 1)) {
				break;
			} else {
				steps++;
				pos = pos + next_steps_num;
			}
		}
	}
	
	if (min_steps == 2147483647) {
		cout<<-1<<endl;
	} else {
		cout<<min_steps<<endl;
	}
	return 0;
}
第三题:
#include<iostream>
#include<unordered_map>
#include<stack>
using namespace std;
unordered_map<string, string> complex_exp;
unordered_map<string, int> easy_exp;
void push_complex_exp(string s) {
	for(int i = 0; i < s.length(); i++) {
		if (s[i] != '=') continue;
		else {
			string s1 = s.substr(0, i);
			string s2 = s.substr(i+ 1);
			complex_exp[s1] = s2;
		}
	}
}

void handle_complex_exp(string exp) {
	int flag = 0;
	for (int i = 0; i < exp.length(); i++) {
		if (i == exp.length() - 1) {
			string s = exp.substr(flag);
			push_complex_exp(s);
		}else if (exp[i] != ',') {
			continue;
		}else {
			string s = exp.substr(flag, i - flag);
			flag = i + 1;
			push_complex_exp(s);	
		}
	}
}

void push_easy_exp(string s) {
	for(int i = 0; i < s.length(); i++) {
		if (s[i] != '=') continue;
		else {
			string s1 = s.substr(0, i);
			string s2 = s.substr(i+ 1);
			int val = atoi(s2.c_str());
			easy_exp[s1] = val;
		}
	}
}

void handle_easy_exp(string exp) {
	int flag = 0;
	for (int i = 0; i < exp.length(); i++) {
		if (i == exp.length() - 1) {
			string s = exp.substr(flag);
			push_easy_exp(s);
		}else if (exp[i] != ',') {
			continue;
		}else {
			string s = exp.substr(flag, i - flag);
			flag = i + 1;
			push_easy_exp(s);	
		}
	}
}

int get_pri(char ch) {
	if (ch == '*' || ch == '/') return 2;
	else return 1;
}

int cal(int num1, int num2, char opt) {
	if (opt == '+') {
		return num1 + num2;
	} else if (opt == '-') {
		return num1 - num2;
	} else if (opt == '*') {
		return num1 * num2;
	} else{
		return num1 / num2;
	}
}

int get_value(string s) {
	stack<int> numbers;
	stack<char> opt;
	int flag = 0;
	for (int i = 0; i < s.length(); i++) {
		if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || i == s.length() - 1) {
			string s_num;
			if (i == s.length() - 1) {
				s_num = s.substr(flag);
			} else {
				s_num = s.substr(flag, i - flag);
				flag = i + 1;
			}
			if (easy_exp.find(s_num) != easy_exp.end()) {
				numbers.push(easy_exp[s_num]);
			} else {
				if(s_num[0] == '{'){
					string s_tmp = s_num.substr(1, s_num.length() - 2);
					numbers.push(atoi(s_tmp.c_str()));
				} else {
					numbers.push(get_value(complex_exp[s_num]));
				}
			}
			
			if (i == s.length() - 1) break;
			
			if (opt.empty()) {
				opt.push(s[i]);
			} else {
				char opt_tmp = opt.top();
				if (get_pri(s[i]) > get_pri(opt_tmp)) {
					opt.push(s[i]);
				} else {
					int number1 = numbers.top();
					numbers.pop();
					int number2 = numbers.top();
					numbers.pop();
					opt.pop();
					int res = cal(number2, number1, opt_tmp);
					numbers.push(res);
					opt.push(s[i]);
				}
			}
		} else {
			continue;
		}
	}
	
	while (!opt.empty()) {
		char opt_tmp = opt.top();
		int number1 = numbers.top();
		numbers.pop();
		int number2 = numbers.top();
		numbers.pop();
		int res = cal(number2, number1, opt_tmp);
		numbers.push(res);
		opt.pop();
	}
	
	int result = numbers.top();
	return result;
}

int main () {
	string exp;
	cin>>exp;
	string s1, s2, s3;
	int flag = 0;
	for (int i = 0; i < exp.length(); i++) {
		if (exp[i] != ';') continue;
		if (flag == 0) {
			s1 = exp.substr(flag, i);
			flag = i + 1;
		} else {
			s2 = exp.substr(flag, i - flag);
			s3 = exp.substr(i + 1);
			break;
		}
	}
	handle_complex_exp(s1);
	handle_easy_exp(s2);
	if (easy_exp.find(s3) != easy_exp.end()) {
		cout<<easy_exp[s3]<<endl;
	} else {
		cout<<get_value(complex_exp[s3]);
	}
	
	return 0;
}


#笔试题目##华为##题解#
全部评论
# 3题  90% if __name__ == "__main__":     bd, id, res = input().split(';')     ids = id.split(',')     for id in ids:         a,b = id.split('=')         bd = bd.replace(a,b)     bds = bd.replace('{','').replace('}','').split(',')     temp_dict = dict([ bd.split('=') for bd in bds])     s = temp_dict[res]     temp_dict.pop(res)     for k,v in temp_dict.items():         s = s.replace(k, '('+v+')')     print(int(eval(s)))
点赞 回复 分享
发布于 2019-09-07 21:32
第二题你们可能理解错了,它必须是围成一个圈,那个圈只能旋转和反转,而不是圈里面的元素随意排列
点赞 回复 分享
发布于 2019-09-07 21:08
第二题不是简单的数学排列问题,玩过dota的都懂。我第二题看完题目,想了想,直接放弃了。然后去做第三题,半个小时写完。
点赞 回复 分享
发布于 2019-09-07 21:18
大佬大佬,我写了第一题就交了😂放弃
点赞 回复 分享
发布于 2019-09-07 21:03
。。调了1个多小时第二题一直是0,都没心情做别的了。。。
点赞 回复 分享
发布于 2019-09-07 21:03
大佬,可以求一下笔试题目吗😷十分感谢
点赞 回复 分享
发布于 2019-09-09 15:33
大佬,能否给一下第3题的输入数据测试案例呀
点赞 回复 分享
发布于 2019-09-07 22:52
第二题,能说说思路么
点赞 回复 分享
发布于 2019-09-07 21:25
170行可太秀了
点赞 回复 分享
发布于 2019-09-07 21:16
有大佬能说一下题目是啥吗😂
点赞 回复 分享
发布于 2019-09-07 21:16
真大佬!
点赞 回复 分享
发布于 2019-09-07 21:04
我也是,第二题一直为0。。。第一题5分钟,第二题1小时55分钟😓就死磕。。
点赞 回复 分享
发布于 2019-09-07 21:04
第二题一直没弄出来都没空看第三题
点赞 回复 分享
发布于 2019-09-07 21:04
我的第二题也一直是0,我手算后都是对的
点赞 回复 分享
发布于 2019-09-07 21:03
给第二题搞自闭了……
点赞 回复 分享
发布于 2019-09-07 21:03
第二题是啥子情况,我感觉没问题,就是0
点赞 回复 分享
发布于 2019-09-07 21:02

相关推荐

评论
1
36
分享

创作者周榜

更多
牛客网
牛客企业服务