2018年华为校招面试机试题目与参考答案解析
crazybean
1.数字处理
题目描述:给出一个不多于5位的整数,进行反序处理,要求
(1)求出它是几位数
(2)分别输出每一个数字(空格隔开)
(3)按逆序输出各位数字(仅数字间以空格间隔,负号与数字之间不需要间隔)
输入描述:位数不大于5的整数
输出描述:1.整数位数 2.空格间隔输出结果 3.逆序整数
#include <vector> #include <string> #include <iostream> using namespace std; int main() { int iInput; int iNum=0; char* strRst=""; int iOutput=0; cin >> iInput; int k = 0; int temp; int flag; int iTemp; if (iInput < -99999 || iInput > 99999) return -1; if (iInput < 0) { flag = -1; iInput = -iInput; } else flag = 1; temp = iInput; vector<int> resStr; while (temp) { ++iNum; iTemp = temp % 10; resStr.push_back(iTemp); iOutput = iOutput * 10 + iTemp; temp /= 10; } cout << iNum << endl; if (flag == -1) cout << "-"; for (int i = iNum - 1; i >= 0; --i) { if (i == iNum - 1) cout << resStr[i]; else { cout << " " << resStr[i]; } } cout << endl; iOutput = flag*iOutput; cout << iOutput << endl; system("pause"); return 0; }
2.IP地址交集判断
题目描述:输入四个IP端,前两个为第一个IP段的起始和终止地址,后两个是第二个IP段的起始和终止地址,判断这两个IP段是否存在交集
输入描述:输入4个IP
输出描述:如果存在交集,输出 Overlap IP ; 如果不存在交集,输出 No Overlap IP
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<cassert> using namespace std; int *dec2bin(int decnum) { int i, a, *b = { 0 }; a = decnum; for (i = 7; i >= 0; i--) { b[i] = a % 2; a = a / 2; } return b; } int ipToInt(char *ipString) { assert(ipString != NULL); int i = 0, j, n, count = 0, return_num = 0; char *tmp; int *tmp_num=NULL, *num=NULL, *d2b; char *s = ipString, *s_tmp=NULL; if (*s == '.') count++; count++; if (count != 4) return 0; while (*s != '\0') { if (*s != '.') { n = s - s_tmp; tmp = (char*)malloc(n*sizeof(char)); memcpy(tmp, s, n); tmp_num[i] = atoi(tmp); d2b = dec2bin(tmp_num[i]); for (j = 0; j<8; j++) num[8 * i + j] = d2b[j]; s++; i++; s_tmp = s; } s++; } if (*s = '\0') { n = s - s_tmp; tmp = (char*)malloc(n*sizeof(char)); memcpy(tmp, s, n); tmp_num[i] = atoi(tmp); d2b = dec2bin(tmp_num[i]); for (j = 0; j<8; j++) num[8 * i + j] = d2b[j]; } for (j = 0; j<32; j++) return_num = return_num * 2 + num[j]; return return_num; } int main(void) { char *s1, *s2, *s3, *s4; s1 = new char; s2 = new char; s3 = new char; s4 = new char; cin >> s1 >> s2 >> s3 >> s4; int n1, n2, n3, n4, i; n1 = ipToInt(s1); n2 = ipToInt(s2); n3 = ipToInt(s3); n4 = ipToInt(s4); if (n4<n1 || n3>n2) cout << "No Overlap IP" << endl; else cout << "Overlap IP" << endl; system("pause"); return 0; }
3.数字排序
题目描述: 给定字符串内有很多正整数,要求对这些正整数进行排序,然后返回排序后指定位置的正整数,排序要求:按照每一个正整数的后三位数字组成的整数进行从小到大排序(1)如果不足三位,则按照实际位数组成的整数进行比较(2)如果相等,则按照输入字符中的原始顺序排序
说明:(1)字符串以‘\0’结尾,仅包含数字、空格(2)字符串内正整数之间以单个空格分隔,字符串首尾没有空格(3)正整数格式为十进制,大小1~1000000,正整数的数字非零开始
输入描述:第一行为一个整数字符串,里面包含若干个整数,以空格分割,第二行为一个整数,即指定的位置
输出描述:输出指定位置的整数
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <string.h> using namespace std; int find_string(const char* input_string, int serial_number, int output_string_max_length, char* output_string) { int size = strlen(input_string); vector<int> vec; int tmp = 0; for (size_t i = 0; i <= size; ++i){ if (input_string[i] == ' ' || i == size){ if (tmp == 0){ return -1; } vec.push_back(tmp); tmp = 0; } else{ tmp = tmp * 10 + input_string[i] - '0'; } } int n = vec.size(); for (int i = 0; i < n - 1; ++i){ for (int j = 0; j < n - i - 1; ++j){ if (vec[j] % 1000 > vec[j + 1] % 1000){ swap(vec[j], vec[j + 1]); } } } if (serial_number > vec.size()){ return -1; } char num[11]; sprintf(num, "%d", vec[serial_number - 1]); int len = strlen(num); cout << num; return 0; } int main() { string s1; getline(cin, s1); int t; char *input_string = const_cast<char *>(s1.data()); int serial_number, output_string_max_length=0; char* output_string = NULL; cin >> serial_number; t=find_string(input_string, serial_number, output_string_max_length, output_string); system("pause"); return 0; }