1.数字处理
题目描述:
给出一个不多于5位的整数,进行反序处理,要求
(1)求出它是几位数
(2)分别输出每一个数字(空格隔开)
(3)按逆序输出各位数字(仅数字间以空格间隔,负号与数字之间不需要间隔)
输入描述:位数不大于5的整数
输出描述:1.整数位数 2.空格间隔输出结果 3.逆序整数
方案:注意正负号。
int main(){
string str;
while (cin >> str){
int index = 0;
if (str[0] == '+' || str[0] == '-'){
index = 1;
}
cout << str.size() - index << endl;
if (index){
cout << str[0];
}
for (int i = str.size() - 1; i >= index; i--){
cout << str[i] << " ";
}
cout << endl;
}
return 0;
}
2.IP重叠
输入四个IP端,前两个为第一个IP段的起始和终止地址,后两个是第二个IP段的起始和终止地址,判断这两个IP段是否存在交集
输入描述:输入4个IP
输出描述:如果存在交集,输出 Overlap IP ; 如果不存在交集,输出 No Overlap IP 。
方案:IP地址 255.255.255.255 转换为数字的公式为:255*256*256*256 + 255*256*256 + 255*256 + 255。
判断[a,b]和[c,d] 是否有交集,若b<c 或者 d > a,则无交集;反之则有交集。
long long ip2num(string ip){
string num[4];
int index = 0;
int i = 0;
while (i<3){
int pIndex = ip.find('.',index);
num[i] = ip.substr(index,pIndex-index);
index = pIndex + 1;
i++;
}
num[3] = ip.substr(index);
long long result = stoul(num[0]) * 256 * 256 * 256 + stoul(num[1]) * 256 * 256 + stoul(num[2]) * 256 + stoul(num[3]);
return result;
}
int main(){
string ip1, ip2, ip3, ip4;
while (cin >> ip1 >> ip2 >> ip3 >> ip4){
long long num1 = ip2num(ip1);
long long num2 = ip2num(ip2);
long long num3 = ip2num(ip3);
long long num4 = ip2num(ip4);
if (num4 < num1 || num2 < num3){
cout << "No Overlap IP" << endl;
}
else{
cout << "Overlap IP" << endl;
}
}
return 0;
}
3.整数排序
给定字符串内有很多正整数,要求对这些正整数进行排序,然后返回排序后指定位置的正整数
排序要求:按照每个正整数的后三位数字组成的整数进行从小到大排序
1)如果不足三位,则按照实际位数组成的整数进行比较
2)如果相等,则按照输入字符串中的原始顺序排序
说明(以下内容考生无须检查,调用者保证):
1) 字符串以’\0’结尾,仅包含数字、空格
2) 字符串内正整数之间以单个空格分隔,字符串首尾没有空格
3) 正整数格式为十进制,大小:1~1000000,正整数的数字非零开始
示例:
如字符串内容
1223 22 3232 2016
按照规定排序后
2016 22 1223 3232
查询排序后的第3个数是
1223
方案1:取数字最后三位数,直接对数字进行%1000运算即可。
排序时,可调用库函数sort(),只需对其cmp函数进行修改即可。
vector<int> getNum(string str){
vector<int> result;
int i = 0;
int index = 0;
while (str[i] != '\0'){
if (str.find(' ', index) == string::npos){
break;
}
int pos = str.find(' ',index);
string tmp = str.substr(index,pos);
result.push_back(atoi(tmp.c_str()));
index = pos + 1;
}
result.push_back(atoi(str.substr(index).c_str()));
return result;
}
bool mcmp(int a,int b){
return (a%1000) < (b%1000);
}
int main(){
string str;
int N;
while (getline(cin,str)){
cin >> N;
vector<int> num = getNum(str);
/*cout << "排序前:" << endl;
for (int i = 0; i < num.size(); i++){
cout << num[i] << " ";
}
cout << endl;*/
sort(num.begin(),num.end(),mcmp);
/*cout << "排序后:" << endl;
for (int i = 0; i < num.size(); i++){
cout << num[i] << " ";
}
cout << endl;*/
cout << num[N-1] << endl;
}
return 0;
}
知识点:字符串和数字转换
int main(){
string str = "016";
long long result1 = atol(str.c_str());
long long result2 = atoll(str.c_str());
/*long long result3 = stol(str);
long long result4 = stoll(str);*/
cout << "result1: " << result1 << endl;
cout << "result2: " << result2 << endl;
/*cout << "result3: " << result3 << endl;
cout << "result4: " << result4 << endl;*/
int n = 2344;
string s = to_string(n);
cout << s << endl;
}
参考链接:
2018华为校招机试题目练习 https://blog.csdn.net/wgx571859177/article/details/79678656
字符串和数字转换:http://c.biancheng.net/view/1527.html 、
C++字符串转换(stoi、stol、stoll、stof等) https://blog.csdn.net/baidu_34884208/article/details/88342844