题解 | #识别有效的IP地址和掩码并进行分类统计#
识别有效的IP地址和掩码并进行分类统计
http://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682
写的一坨屎,记录一下--
#include<bits/stdc++.h>
using namespace std;
int string2int(string str);
int ip2numvec(vector<int>& nums,string str);
void count_1(string str1,string str2);
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int wrong=0;
int private_=0;
int main()
{
string str;
while(cin>>str)
{
int index = str.find('~');
string str1 = str.substr(0,index);
string str2 = str.substr(index+1);
count_1(str1,str2);
}
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<wrong<<" "<<private_;
return 0;
}
void count_1(string str1,string str2)
{
vector<int> nums;
vector<int> nums2;
char res;
if(ip2numvec(nums,str1)&&ip2numvec(nums2,str2))
{
if(nums[0]==0||nums[0]==127)
return;
if(nums[0]>=1&&nums[0]<=126&&nums[1]>=0&&nums[1]<=255&&nums[2]>=0&&nums[2]<=255&&
nums[3]>=0&&nums[3]<=255)
res='A';
else if(nums[0]>=128&&nums[0]<=191&&nums[1]>=0&&nums[1]<=255&&nums[2]>=0&&nums[2]<=255&&
nums[3]>=0&&nums[3]<=255)
res = 'B';
else if(nums[0]>=192&&nums[0]<=223&&nums[1]>=0&&nums[1]<=255&&nums[2]>=0&&nums[2]<=255&&
nums[3]>=0&&nums[3]<=255)
res='C';
else if(nums[0]>=224&&nums[0]<=239&&nums[1]>=0&&nums[1]<=255&&nums[2]>=0&&nums[2]<=255&&
nums[3]>=0&&nums[3]<=255)
res = 'D';
else if(nums[0]>=240&&nums[0]<=255&&nums[1]>=0&&nums[1]<=255&&nums[2]>=0&&nums[2]<=255&&
nums[3]>=0&&nums[3]<=255)
res='E';
else
res='W';
char res3='W';
if(nums[0]==10&&nums[1]>=0&&nums[1]<=255&&nums[2]>=0&&nums[2]<=255&&
nums[3]>=0&&nums[3]<=255)
res3='P';
if(nums[0]==172&&nums[1]>=16&&nums[1]<=31&&nums[2]>=0&&nums[2]<=255&&
nums[3]>=0&&nums[3]<=255)
res3='P';
if(nums[0]==192&&nums[1]==168&&nums[2]>=0&&nums[2]<=255&&
nums[3]>=0&&nums[3]<=255)
res3='P';
string restr="";
for(int i=0; i<nums2.size(); ++i)
{
int m = nums2[i];
string str1="";
if(m==0)
str1="00000000";
while(m)
{
if(m%2==1)
str1.insert(str1.size(),1,'1');
else
str1.insert(str1.size(),1,'0');
m=m/2;
}
while(str1.size()<8){
str1.insert(str1.size(),1,'0');
}
reverse(str1.begin(), str1.end());
restr+=str1;
}
char res2='R';
int flag=0;
int num_1=0;
int num_0=0;
for(int i=0; i<restr.size(); ++i)
{
if(restr[i]=='0')
{
flag = 1;
}
if(flag==1&&restr[i]=='1')
res2='W';
if(restr[i]=='0')
num_0++;
else if(restr[i]=='1')
num_1++;
}
if(num_0==0||num_1==0)
res2='W';
if(res=='W'||res2=='W'){
if(res=='W'&&res2=='W')
wrong+=2;
else
wrong++;
return;
}else{
if(res=='A')
a++;
else if(res=='B')b++;
else if(res=='C')c++;
else if(res=='D')
{
d++;
}
else if(res=='E')e++;
if(res3=='P')private_++;
}
}
return;
}
int ip2numvec(vector<int>& nums,string str){
vector<string> vec;
int start = 0;
for(int i=0;i<str.length();++i){
if(str[i]=='.'){
int len = i-start;
if(len<1){
wrong++; return 0;
}
vec.push_back(str.substr(start,len));
start = i+1;
}
}
if(vec.size()!=3){
wrong++;return 0;
}
vec.push_back(str.substr(start));
for(int i=0;i<vec.size();++i){
int tmp = string2int(vec[i]);
nums.push_back(tmp);
}
return 1;
}
int string2int(string str){
int res=0;
for(int i=0;i<str.size();++i){
res = (str[i]-'0') + res*10;
}
return res;
}