華為2018機試題
2018
1.數字處理
題目描述:給出一個不多於5位的整數,進行反序處理,要求
(1)求出它是幾位數
(2)分別輸出每一個數字(空格隔開)
(3)按逆序輸出各位數字(僅數字間以空格間隔,負號與數字之間不需要間隔)
輸入描述:位數不大於5的整數
輸出描述:1.整數位數 2.空格間隔輸出結果 3.逆序整數
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int num, flag,temp_num,output_num=0;
vector<int> vector_num;
cin >> num;
if (num < -99999 || num > 99999)
{
return -1;
}
if (num < 0)
{
flag = -1;
temp_num = -num;
}
else
{
flag = 1;
temp_num = num;
}
int count = 0;
while (temp_num)
{
count++;
int i = temp_num % 10;
temp_num = temp_num / 10;
output_num = i + output_num*10;
vector_num.push_back(i);
}
cout << count << endl;
if (flag == -1)
cout << "-";
for (int j = count-1; j >= 0; j--)
{
if (j == count - 1)
cout << vector_num[j];
else
cout << " " << vector_num[j];
}
cout << endl;
output_num = flag*output_num;
cout << output_num << endl;
system("pause");
return 0;
}
2.IP地址交集判斷
題目描述:輸入四個IP端,前兩個為第一個IP段的起始和終止地址,後兩個是第二個IP段的起始和終止地址,判斷這兩個IP段是否存在交集
輸入描述:輸入4個IP
輸出描述:如果存在交集,輸出 Overlap IP ; 如果不存在交集,輸出 No Overlap IP
#include<iostream>
#include<string>
using namespace std;
long long string_to_num(string str)
{
string s[4];
long long num;
int i = 0,index=0;
while (i < 3)
{
int pos = str.find('.', index);
s[i++] = str.substr(index, pos - index);
index = pos + 1;
}
s[3] = str.substr(index);
num = stoul(s[0]) * 256 * 256 * 256 + stoul(s[1]) * 256 * 256 + stoul(s[2]) * 256 + stoul(s[3]);
return num;
}
int main()
{
string s1, s2, s3, s4;
long long first_begin, first_end, second_begin, second_end;
while (cin >> s1 >> s2 >> s3 >> s4)
{
first_begin = string_to_num(s1);
first_end = string_to_num(s2);
second_begin = string_to_num(s3);
second_end = string_to_num(s4);
if (first_begin > second_end || first_end < second_begin)
cout << "No overlap IP" << endl;
else
cout << "Overlap IP" << endl;
}
return 0;
}
3.數字排序
題目描述: 給定字串內有很多正整數,要求對這些正整數進行排序,然後返回排序後指定位置的正整數,排序要求:按照每一個正整數的後三位數字組成的整數進行從小到大排序(1)如果不足三位,則按照實際位陣列成的整數進行比較(2)如果相等,則按照輸入字元中的原始順序排序
說明:(1)字串以‘\0’結尾,僅包含數字、空格(2)字串內正整數之間以單個空格分隔,字串首尾沒有空格(3)正整數格式為十進位制,大小1~1000000,正整數的數字非零開始
輸入描述:第一行為一個整數字符串,裡面包含若干個整數,以空格分割,第二行為一個整數,即指定的位置
輸出描述:輸出指定位置的整數
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
vector<string> splitstr(string s)
{
vector<string> num;
int length = s.length();
int index = 0;
while (index < length)
{
int pos = s.find(' ',index);
if (pos == string::npos)
break;
num.push_back(s.substr(index, pos - index));
index = pos + 1;
}
num.push_back(s.substr(index));
return num;
}
vector<int> str2num(vector<string> number)
{
int length = number.size();
vector<int> n;
for (int i = 0; i < length; i++)
{
int len = number[i].length();
string sub=number[i];
if (len > 3)
sub = number[i].substr(len-3);
n.push_back(stoi(sub));
}
return n;
}
vector<string> sort(vector<string> number, vector<int> num)
{
int length = num.size();
for (int i = 0; i < length - 1; i++)
{
int j = i + 1;
int index = i;
for (int j = i + 1; j < length; j++)
{
if (num[j] < num[index])
{
index = j;
}
}
swap(num[i], num[index]);
swap(number[i], number[index]);
}
return number;
}
int main()
{
int N;
string str;
while (getline(cin, str))
{
cin >> N;
vector<string> num = splitstr(str);
vector<int> subnum = str2num(num);
num = sort(num, subnum);
cout << num[N - 1] << endl;
}
return 0;
}