华为笔试软件岗位新鲜出炉
华为笔试第一题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; }