华为2019/04/10春招实习生笔试题

前两题比较基础,第三题也不是很难,刚做完,记录一下,04/13更新第三题做法

————————————————————————————————

第一题:

输入的字符串超过8个字符的,按8个截一段,最后不足8个的补0到8个

最后将重新得到的字符串按升序排列

输入描述:输入一个数字N ,N个字符串 ,中间以空格隔开

输出描述:排序后的字符串

例:输入:2  abc 123456789    输出: 12345678 90000000 abc00000

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 
 5 using namespace std;
 6 
 7 int main() {
 8     int num;
 9     while (cin >> num)
10     {
11         //vector<string> str_array;
12         vector<string> str_array_new;
13         for (int i = 0; i < num; i++) {
14             string str;
15             cin >> str;
16             //str_array.push_back(str);
17             while (str.length() > 8)
18             {
19                 string str_temp(str.begin(), str.begin() + 8);
20                 str_array_new.push_back(str_temp);
21                 str.erase(str.begin(),str.begin() + 8);
22             }
23             while (str.length() < 8)
24             {
25                 str.push_back('0');
26             }
27             str_array_new.push_back(str); 
28         }
29         int length = str_array_new.size();
30         for (int i = 0;  i < length; i++)
31         {
32             for (int j = i + 1 ; j < length; j++)
33             {
34                 if (str_array_new[j][0] < str_array_new[i][0]) {
35                     string temp = str_array_new[i];
36                     str_array_new[i] = str_array_new[j];
37                     str_array_new[j] = temp;
38                 }
39             }
40         }
41         for (int i = 0; i < length; i++)
42         {
43             cout << str_array_new[i] << ' ';
44         }
45         cout << endl;
46     }
47 
48     system("pause");
49     return 0;
50 }

第二题:

题目描述:输入一个字符串,含有括号(大括号,小括号,中括号),数字和字母,数字(n)之后必跟一个括号(测试用例里的括号都是匹配的),代表括号内的字符串重复(n)次。括号里可以有嵌套,即括号里含有括号。现在将输入的字符串逆序展开;

输入描述:字符串,例:abc3(A)

输出描述:字符串,例:AAAcba

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<algorithm>
 5 
 6 using namespace  std;
 7 
 8 int have_kuohao(string str) {
 9     for (int i = 0; i < str.length(); i++)
10     {
11         if (str[i] == '(' || str[i] == '[' || str[i] == '{')
12         {
13             return 1;
14         }
15         
16     }
17     return 0;
18 }
19 int main()
20 {
21     string str;
22     while (cin >> str)
23     {
24         //括号嵌套问题
25         //没有括号直接输出
26         if (have_kuohao(str) == 0)
27         {
28             reverse(str.begin(),str.end());
29             cout << str << endl;
30             continue;
31         }
32         else
33         {
34             //
35             while (have_kuohao(str))
36             {
37                 int left;
38                 int right;
39                 for (int i = 0; i < str.length(); i++)
40                 {
41                     if (str[i] == '(' || str[i] == '[' || str[i] == '{')
42                     {
43                         left = i;
44                     }
45                     if (str[i] == ')' || str[i] == ']' || str[i] == '}')
46                     {
47                         right = i;
48                         break;
49                     }
50                 }
51                 //展开插入
52                 int times = (int)(str[left - 1] - '0');
53                 string temp_str(str.begin() + left + 1, str.begin() + right);
54                 //在右括号后插入
55                 int insert_end = right;
56                 for (int i = 0; i < times - 1; i++)
57                 {
58                     for (int j = 0; j < temp_str.length(); j++)
59                     {
60                         str.insert(str.begin() + insert_end + 1, temp_str[j]);
61                         //cout << str << endl;
62                         insert_end ++;
63                     }
64                 }
65                 //删除括号和数字
66                 str.erase(str.begin() + right);
67                 str.erase(str.begin() + left);
68                 str.erase(str.begin() + left - 1 );
69             }
70             reverse(str.begin(), str.end());
71             //倒序输出
72             cout << str <<endl;
73         }
74     }
75 
76 
77     system("pause");
78     return 0;
79 }

第三题:时间不够了,没写出来

题目描述:

输入一个N*M矩阵栅格,每个栅格都有一个高程值代表海拔高度,小明从出发点到终点有几条不同路径?每次只能往更高海拔的地方去,走过的地方不能再走,只能前后左右走。

例:输入:

5 4

0 1 0 0

0 2 3 3

0 3 0 0

0 4 5 6

0 7 6 0

0 1 4 1

第一行代表M*N,网格大小

后面是M*N的矩阵

最后一行前两个数字代表 起始坐标,后面两个数字代表 目的坐标(坐标从左上角(0,0)开始)。

输出 :2

即两条不同路径

解法:

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<stack>
 5 using namespace std;
 6 
 7 
 8 //建立一个结构体
 9 
10 struct grid {
11     int high = 0;
12     //flag为0代表未到达过
13     int arived_flag = 0;
14 };
15 //深度优先遍历搜索函数
16 int DFT_result(vector<vector<grid>> map,int m,int n, int des_zuobiao[2]) {
17     int result = 0;
18     int M = map.size();
19     int N = map[0].size();
20     if (m == des_zuobiao[0] && n == des_zuobiao[1])
21     {
22         result++;
23         return result;
24     }
25     else
26     {
27         //当前位置设置为已走过
28         map[m][n].arived_flag = 1;
29         //判断是否越界,上下左右邻接点
30         if (m - 1 >= 0)
31         {
32             if (map[m - 1][n].arived_flag != 1 && map[m - 1][n].high > map[m][n].high)
33             {
34                 result += DFT_result(map, m - 1, n, des_zuobiao);
35             }
36         }
37         if (m + 1 <= M - 1)
38         {
39             if (map[m + 1][n].arived_flag != 1 && map[m + 1][n].high > map[m][n].high)
40             {
41                 result += DFT_result(map, m + 1, n, des_zuobiao);
42             }
43         }
44         if (n - 1 >= 0)
45         {
46             if (map[m][n - 1].arived_flag != 1 && map[m][n - 1].high > map[m][n].high)
47             {
48                 result += DFT_result(map, m, n - 1, des_zuobiao);
49             }
50         }
51         if (n + 1 <= N - 1)
52         {
53             if (map[m][n + 1].arived_flag != 1 && map[m][n + 1].high > map[m][n].high)
54             {
55                 result += DFT_result(map, m, n + 1, des_zuobiao);
56             }
57         }
58         
59     }
60 
61 
62     return result;
63 }
64 int main(){
65     int map_M = 0;
66     int map_N = 0;
67     cin >> map_M;
68     cin >> map_N;
69 
70     vector<vector<grid>> map;
71     for (int i = 0; i < map_M; i++)
72     {
73         vector<grid> row;
74         for (int j = 0; j < map_N; j++) {
75             int high = 0;
76             cin >> high;
77             grid grid_temp{ high, 0 };
78             row.push_back(grid_temp);
79             //cin >> map[map_M][map_N];
80         }
81         map.push_back(row);
82     }
83 
84     int pre_zuobiao[2];
85     int des_zuobiao[2];
86     cin >> pre_zuobiao[0];
87     cin >> pre_zuobiao[1];
88     cin >> des_zuobiao[0];
89     cin >> des_zuobiao[1];
90 
91     int result = DFT_result(map, pre_zuobiao[0], pre_zuobiao[1], des_zuobiao);
92     cout << result << endl;
93     system("pause");
94 }
原文地址:https://www.cnblogs.com/lijiaxin/p/10686491.html