1.数字处理
题目描述:给出一个不多于5位的整数,进行反序处理,要求
(1)求出它是几位数
(2)分别输出每一个数字(空格隔开)
(3)按逆序输出各位数字(仅数字间以空格间隔,负号与数字之间不需要间隔)
输入描述:位数不大于5的整数
输出描述:1.整数位数 2.空格间隔输出结果 3.逆序整数
#include
#include
using namespace std;
int main()
{
string str;
char c;
bool sign = false;
while (cin >> str){
int length = str.length();
if (str[0] == '-' || str[0] == '+'){
length--;
c = str[0];
sign = true;
str = str.substr(1, length);
}
cout << length << endl;
for (int i = 0; i < length; i++){
cout << str[i] << " ";
}
cout << endl;
if (sign){
cout << c;
}
for (int i = length - 1; i >= 0; i--){
cout << str[i];
}
cout << endl;
}
}
2
输入四个IP端,前两个为第一个IP段的起始和终止地址,后两个是第二个IP段的起始和终止地址,判断这两个IP段是否存在交集
输入描述:输入4个IP
输出描述:如果存在交集,输出 Overlap IP ; 如果不存在交集,输出 No Overlap IP
注意
数值范围,int为2^15-1,long为2^31-1,unsigned long为2^32-1,long long为2^63-1
long long str2num(string str)
{
int length = str.length();
string s[4];
int i = 0;
int 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);
long long num = stoul(s[0]) * 256 * 256 * 256 + stoul(s[1]) * 256 * 256 + stoul(s[2]) * 256 + stoul(s[3]);
return num;//注意数值范围,int为2^15-1,long为2^31-1,unsigned long为2^32-1
}
int main()
{
string ipTop1, ipEnd1, ipTop2, ipEnd2;
while (cin >> ipTop1>>ipEnd1>> ipTop2>> ipEnd2){
long long top1 = str2num(ipTop1);
long long end1 = str2num(ipEnd1);
long long top2 = str2num(ipTop2);
long long end2 = str2num(ipEnd2);
//cout << top1 << " " << end1 << " " << top2 << " " << end2 << endl;
if (top1 > end2 || end1 < top2){
cout << "No Overlap IP" << endl;
}
else
cout << "Overlap IP" << endl;
}
}
3
题目
给定字符串内有很多正整数,要求对这些正整数进行排序,然后返回排序后指定位置的正整数
排序要求:按照每个正整数的后三位数字组成的整数进行从小到大排序
1)如果不足三位,则按照实际位数组成的整数进行比较
2)如果相等,则按照输入字符串中的原始顺序排序
说明(以下内容考生无须检查,调用者保证):
1) 字符串以’\0’结尾,仅包含数字、空格
2) 字符串内正整数之间以单个空格分隔,字符串首尾没有空格
3) 正整数格式为十进制,大小:1~1000000,正整数的数字非零开始
示例:
如字符串内容
1223 22 3232 2016
按照规定排序后
2016 22 1223 3232
查询排序后的第3个数是
1223
一个数的后三位可以直接用 num%1000就可以了,就取到了后3位
//3题
//思路一,比较明显的桶排序
vector splitstr(string str)
{
vector num;
int length = str.length();
int index = 0;
while (index Sortnum(vector num,int e)
{
int output[100]; //涉及到不按顺序索引时,用数组
int length = num.size();
int bucket[10] = { 0 };
for (int i = 0; i < length; i++)
bucket[num[i] / e % 10]++;
for (int i = 1; i < 10; i++)
bucket[i] += bucket[i - 1];
for (int i = length - 1; i >= 0; i--)
output[--bucket[num[i] / e % 10]] = num[i];
for (int i = 0; i < length; i++)
num[i] = output[i];
return num;
}
int main()
{
string str;
int N;
while (getline(cin,str)){//输入一行字符可以用getline获得
cin >> N;
vector num = splitstr(str);
for (int e = 1; e <= 100; e *= 10){
num = Sortnum(num, e);
}
cout << num[N - 1] << endl;
}
}
//思路二,用简单的选择排序来做
vector splitstr1(string str)
{
vector num;
int length = str.length();
int index = 0;
while (index str2num(vector num)
{
int length = num.size();
vector n;
for (int i = 0; i < length; i++){
int len = num[i].length();
string sub = num[i];
if (len>3)
sub = num[i].substr(len - 3);
n.push_back(stoi(sub));
}
return n;
}
vector Sortstr(vectornum, vector subnum)
{
int length = subnum.size();
for (int i = 0; i < length - 1; i++){
int j = i + 1;
int index = i;
for (j = i + 1; j < length; j++){
if (subnum[j]> N;
vector num = splitstr1(str);
vector subnum = str2num(num);
num=Sortstr(num, subnum);
cout << num[N - 1] << endl;
}
}