2018
1.数字处理
题目描述:给出一个不多于5位的整数,进行反序处理,要求
(1)求出它是几位数
(2)分别输出每一个数字(空格隔开)
(3)按逆序输出各位数字(仅数字间以空格间隔,负号与数字之间不需要间隔)
输入描述:位数不大于5的整数
输出描述:1.整数位数 2.空格间隔输出结果 3.逆序整数
#include
#include
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
#include
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
#include
#include
#include
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;
}