题解 | #识别有效的IP地址和掩码并进行分类统计#
识别有效的IP地址和掩码并进行分类统计
https://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682
#include <iostream>
#include<string>
#include<vector>
using namespace std;
//分割
//判断
//输出
vector<string> split(string str, const char c) {
vector<string> vstr;
int posStart = 0;
while (posStart < str.length()) {
int posEnd;
for (posEnd = posStart; str[posEnd] != c && posEnd < str.length(); posEnd++);
vstr.push_back(str.substr(posStart, posEnd - posStart));
posStart = posEnd + 1;
}
return vstr;
}
unsigned long valueIP(int x0, int x1, int x2, int x3) {
unsigned long value_IP = x0 * 255 * 255 * 255 + x1 * 255 * 255 + x2 * 255 + x3;
return value_IP;
}
char typeIP(string str) {
vector<string> vstr = split(str, '.');
if (vstr.size() == 4) {
int num0 = atoi(vstr[0].c_str());
int num1 = atoi(vstr[1].c_str());
int num2 = atoi(vstr[2].c_str());
int num3 = atoi(vstr[3].c_str());
if ((num0 > 0 && num0 <= 255) && (num1 >= 0 && num1 <= 255) && (num2 >= 0 &&
num2 <= 255) && (num3 >= 0 && num3 <= 255)) {
unsigned long value = valueIP(num0, num1, num2, num3);
if ((value >= valueIP(1, 0, 0, 0)) && value <= valueIP(126, 255, 255, 255)) {
if ((value >= valueIP(10, 0, 0, 0)) && value <= valueIP(10, 255, 255, 255)) {
return 'a';
}
return 'A';
}
if ((value >= valueIP(128, 0, 0, 0)) && value <= valueIP(191, 255, 255, 255)) {
if ((value >= valueIP(172, 16, 0, 0)) && value <= valueIP(172, 31, 255, 255)) {
return 'b';
}
return 'B';
}
if ((value >= valueIP(192, 0, 0, 0)) && value <= valueIP(223, 255, 255, 255)) {
if ((value >= valueIP(192, 168, 0, 0)) &&
value <= valueIP(192, 168, 255, 255)) {
return 'c';
}
return 'C';
}
if ((value >= valueIP(224, 0, 0, 0)) && value <= valueIP(239, 255, 255, 255)) {
return 'D';
}
if ((value >= valueIP(240, 0, 0, 0)) && value <= valueIP(255, 255, 255, 255)) {
return 'E';
}
return 'X';
}
return 'X';
}
return 'X';
}
bool isOk(int num){
int a=num-128;
if(a!=0){
a-=64;
if(a!=0){
a-=32;
if(a!=0){
a-=16;
if(a!=0){
a-=8;
if(a!=0){
a-=4;
if(a!=0){
a-=2;
if(a!=0){
a-=1;
if(a!=0){
return false;
}else{
return true;
}
}else{
return true;
}
}else{
return true;
}
}else{
return true;
}
}else{
return true;
}
}else{
return true;
}
}else{
return true;
}
}else{
return true;
}
}
bool isSubNetMask(string str){
vector<string> vstr = split(str, '.');
if (vstr.size() == 4) {
int num0 = atoi(vstr[0].c_str());
int num1 = atoi(vstr[1].c_str());
int num2 = atoi(vstr[2].c_str());
int num3 = atoi(vstr[3].c_str());
// 1 2 4 8 16 32 64 128
if ((num0 > 0 && num0 <= 255) && (num1 >= 0 && num1 <= 255) && (num2 >= 0 &&
num2 <= 255) && (num3 >= 0 && num3 <= 255)) {
if(num0==255){
if(num1==255){
if(num2==255){
if(num3==255){
return false;
}else{
if(isOk(num3)||num3==0){
return true;
}
}
return false;
}else{
if(num3==0){
if(isOk(num2)||num2==0){
return true;
}
}
return false;
}
}else{
if(num2==0&&num3==0){
if(isOk(num1)||num1==0){
return true;
}
}
return false;
}
}else{
if(num1==0&&num2==0&&num3==0){
if(isOk(num0)){
return true;
}
}
return false;
}
}
}
return false;
}
int main() {
string str;
int numA=0,numB=0,numC=0,numD=0,numE=0,numError=0,numPrivate=0;
int j=1;
while (getline(cin, str)) {
vector<string> vStrs=split(str, '~');
if(vStrs.size()==2){
//cout<<j<<" -- ";
if(isSubNetMask(vStrs[1])){
switch(typeIP(vStrs[0])){
case 'A':
numA++;
//cout<<vStrs[0]<<" : "<<'A'<<" "<<numA<<endl;
break;
case 'a':
numA++;
numPrivate++;
//cout<<vStrs[0]<<" : "<<'a'<<" "<<numA<<" "<<numPrivate<<endl;
break;
case 'B':
numB++;
//cout<<vStrs[0]<<" : "<<'B'<<" "<<numB<<endl;
break;
case 'b':
numB++;
numPrivate++;
//cout<<vStrs[0]<<" : "<<'b'<<" "<<numB<<" "<<numPrivate<<endl;
break;
case 'C':
numC++;
//cout<<vStrs[0]<<" : "<<'C'<<" "<<numC<<endl;
break;
case 'c':
numC++;
numPrivate++;
//cout<<vStrs[0]<<" : "<<'c'<<" "<<numC<<" "<<numPrivate<<endl;
break;
case 'D':
numD++;
//cout<<vStrs[0]<<" : "<<'D'<<" "<<numD<<endl;
break;
case 'E':
numE++;
//cout<<vStrs[0]<<" : "<<'E'<<" "<<numE<<endl;
break;
case 'X':
//cout<<vStrs[0]<<" : "<<"X-numError"<<endl;
break;
}
}else{
if(typeIP(vStrs[0])!='X'){
numError++;
//cout<<vStrs[1]<<" :numError "<< numError<<endl;
}
}
j++;
}
}
//5 4 2 0 0 11 0
cout<<numA<<' '<<numB<<' '<<numC<<' '<<numD<<' '<<numE<<' '<<numError<<' '<<numPrivate<<endl;
}
#如何看待2023届秋招##我的秋招结束啦##华为2023秋招求职进度交流##市场营销人求职交流聚集地#