华为2019年8月校招秋招笔试机考题解答C++实现
试题时间:2019年7月31日
考核题型:3道大题,第一题100分,第二题200分,第三题300分,总分600
GitHub源码
https://github.com/ioMayday/shiyanlou/tree/master/Interview
题目一:生活实际应用
题目
代码已测试通过,具体如下:
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n, m, i, j;
int stand = 1;
double d,dis_d;
double min_d = 10000;
cin >> d;
if (d >= 10 || d <= 0) return 0;
for (i=1;i<=10000;i++) //对10000件进行遍历,找到对应的售价
{ // i->n
j = i * d - stand;
while (j < i * d + stand){ //与预期值相比较,在小范围内循环
dis_d = (double)(j)/(double)(i) - d; //j->m
if (dis_d < 0) dis_d = -1 * dis_d;
if (dis_d < min_d) {
min_d = dis_d;
n = i;
m = j;
}
else if(dis_d == min_d) {
n = n > i ? i : n;
}
j++;
}
}
cout << m << " " << n << endl;
return 0;
}
题目二:重复子字符串查找
题目描述:
string a="AABBCCDD"; // 源字符串 string b="BBCCDDAA"; // 待测是否被包含 string c="ABCDE"; //AaCDD AaCDD string d="FGDER"; //AaCDD AaCDDF string e="FGRTHJ"; string f="JFR";
输入六行字符串,第一行的字符串可以移位,如AABBCCDD,BBCCDDAA,都是可循环的算作字符串里的,第二行的是待测字符串,看是否被包涵在第一行可循环移位字符串中,是输出1,否则输出0.第三、四行,第五、六行是类似的。若输出为110,则表示前两行匹配包涵,中间两行也匹配包涵,最后两行不匹配包涵。关键核心是实现1、2行的判断。
代码中已有详尽的解释,具体就可以看出其中解题思路。
代码已测试通过,具体如下:
#include <iostream>
#include <string>
//#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
string a,b,c,d,e,f;
string result1,result2,result3;
cin >> a >> b >>c>>d>>e>>f;
//测试用例
// string a="AABBCCDD"; // 源字符串
// string b="BBCCDDAA"; // 待测是否被包含
// string c="ABCDE"; //AaCDD AaCDD
// string d="FGDER"; //AaCDD AaCDDF
// string e="FGRTHJ";
// string f="JFR";
// cout << a << endl;
// cout << b << endl;
// cout << c << endl;
// cout << d << endl;
// cout << e << endl;
// cout << f << endl;
//第1、2行检测
int start_pos = -1;
auto a_position = find( a.begin(), a.end(), b[0] ); //找到对应值的位置 len_position为len的迭代器
int dist = distance(a.begin( ),a_position);
//如果首字母查找不存在
if (a_position == a.end()) result1 = "0";
else if (dist>=0){ //如果首字母匹配存在
start_pos = dist; //start_pos得到的是下标
int i=1;
while(i<b.length())
{
//如果start_pos已到达下标末尾,则不能再进行if,++start_pos,需要回到首尾
if (start_pos == a.length()-1) start_pos = -1; //因为下一步是先加再运算,所以要下标为0回到首位的话,设成-1
if (b[i] != a[++start_pos]) break;
i++;
//cout << start_pos << endl;
}
if (i==b.length()) result1 = "1";
else result1 = "0";
}
//第3、4行检测 与1、2行检测完全类似
start_pos = -1;
auto c_position = find( c.begin(), c.end(), d[0] ); //找到对应值的位置 len_position为len的迭代器
dist = distance(c.begin( ),c_position);
//如果首字母查找不存在
if (c_position == c.end()) result2 = "0";
else if (dist>=0){ //如果首字母匹配存在
start_pos = dist; //start_pos得到的是下标
int i=1;
while(i<d.length())
{
//如果start_pos已到达下标末尾,则不能再进行if,++start_pos,需要回到首尾
if (start_pos == c.length()-1) start_pos = -1; //因为下一步是先加再运算,所以要下标为0回到首位的话,设成-1
if (d[i] != c[++start_pos]) break;
i++;
//cout << start_pos << endl;
}
if (i==d.length()) result2 = "1";
else result2 = "0";
}
//第5、6行检测
start_pos = -1;
auto e_position = find( e.begin(), e.end(), f[0] ); //找到对应值的位置 len_position为len的迭代器
dist = distance(e.begin( ),e_position);
//如果首字母查找不存在
if (e_position == e.end()) result3 = "0";
else if (dist>=0){ //如果首字母匹配存在
start_pos = dist; //start_pos得到的是下标
int i=1;
while(i<f.length())
{
//如果start_pos已到达下标末尾,则不能再进行if,++start_pos,需要回到首尾
if (start_pos == e.length()-1) start_pos = -1; //因为下一步是先加再运算,所以要下标为0回到首位的话,设成-1
if (f[i] != e[++start_pos]) break;
i++;
}
if (i==f.length()) result3 = "1";
else result3 = "0";
}
string result = result1+result2+result3;
cout << result << endl; //result.c_str() 涉及到char数组和string的转换
return 0;
}
题目三:拓扑图的遍历,没做
题目展示如下: