题解 | #识别有效的IP地址和掩码并进行分类统计#

识别有效的IP地址和掩码并进行分类统计

http://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682

主要就是mask判断前面是否全为1,还有注意127和1这里。

#include <string>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

    //bool A=false,B=false,C=false,D=false,E=false,fIP_MSK=false;
    unsigned net=0;

bool judge_ip(string &str);
bool judge_mask(string &str);
void privt_net(string &str,unsigned&net);

bool judge_ip(string &str){
    
    vector<string> v;
    string temp;
    stringstream ss1(str);
    while(getline(ss1,temp,'.')) v.push_back(temp);
    {
        
        if(v[0].empty()){
            return false;
            
        }
        else if(stoi(v[0])==0||stoi(v[0])==127){
            return false;
        }
        else if(v[1].empty()||v[2].empty()||v[3].empty()){
            return false;
            
        }
        else if(stoi(v[1])>=0&&stoi(v[1])<=255&&
                stoi(v[2])>=0&&stoi(v[2])<=255&&stoi(v[3])>=0&&stoi(v[3])<=255){
            if(stoi(v[0])>=1&&stoi(v[0])<=126){
               
                return true;
            }
            if(stoi(v[0])>=127&&stoi(v[0])<=191){
                
                return true;
            }
            if(stoi(v[0])>=192&&stoi(v[0])<=223){
                
                return true;
            }
            if(stoi(v[0])>=224&&stoi(v[0])<=239){
                
                return true;
            }
            if(stoi(v[0])>=240&&stoi(v[0])<=255){
                
                return true;
            }
            else {
                return false;
                
            }
        }
        else{
            return false;
        }
        return false;
        
    }
   
}

void privt_net(string &str,unsigned&net){
    vector<string> v;
    string temp;
    stringstream ss1(str);
    while(getline(ss1,temp,'.')) v.push_back(temp);
    if(stoi(v[0])==10&&stoi(v[1])>=0&&stoi(v[1])<=255&&
      stoi(v[2])>=0&&stoi(v[2])<=255&&stoi(v[3])>=0&&stoi(v[3])<=255){
        net++;
    }
    if(stoi(v[0])==172&&stoi(v[1])>=16&&stoi(v[1])<=31&&
      stoi(v[2])>=0&&stoi(v[2])<=255&&stoi(v[3])>=0&&stoi(v[3])<=255){
        net++;
    }
    if(stoi(v[0])==192&&stoi(v[1])==168&&
      stoi(v[2])>=0&&stoi(v[2])<=255&&stoi(v[3])>=0&&stoi(v[3])<=255){
        net++;
    }
}

bool judge_mask(string &str){
    string temp;
    istringstream ss1(str);
    unsigned n=0;
    
    
    if(judge_ip(str)){
    
    while(getline(ss1,temp,'.')) n=(n<<8)+stoi(temp);
        //cout<<~n;
    n=~n+1;
        //cout<<n;
    if(n==1) {return false;}
    else if((n&(n-1))!=0) {return false;}
    else return true;
    }
    return false;
    
}



int main() {
    string str,strtp;
    unsigned A1=0,B1=0,C1=0,D1=0,E1=0,FIP=0;
    
    while(getline(cin,str)){
    stringstream ss(str);
    vector<string>  v;
    
    while(getline(ss,strtp,'~')) v.push_back(strtp);
    
        if(judge_ip(v[0])){
            
            
            if(judge_mask(v[1])){
                privt_net(v[0], net);
                vector<string> v;
                string temp;
                stringstream ss1(str);
                while(getline(ss1,temp,'.')) v.push_back(temp);

            if(stoi(v[0])>=1&&stoi(v[0])<=126){
               
                A1++;
            }
            if(stoi(v[0])>=127&&stoi(v[0])<=191){
                
                B1++;
            }
            if(stoi(v[0])>=192&&stoi(v[0])<=223){
                
               C1++;
            }
            if(stoi(v[0])>=224&&stoi(v[0])<=239){
                
                D1++;
            }
            if(stoi(v[0])>=240&&stoi(v[0])<=255){
                
                E1++;
            }
    }
            else FIP++;
  }
        else{
            vector<string> v;
            string temp;
            stringstream ss1(str);
            while(getline(ss1,temp,'.')) v.push_back(temp);
            
            if(v[0].empty())    FIP++;
        else if(stoi(v[0])==0||stoi(v[0])==127);
        else if(v[1].empty()||v[2].empty()||v[3].empty())    FIP++;
        else if(stoi(v[1])<0&&stoi(v[1])>255&&
                stoi(v[2])<0&&stoi(v[2])>255&&stoi(v[3])<0&&stoi(v[3])>255)    FIP++;
            else    FIP++;
    }
            
            //cout<<A1<<' '<<B1<<' '<<C1<<' '<<D1<<' '<<E1<<' '<<FIP<<' '<<net<<endl;
        }
    cout<<A1<<' '<<B1<<' '<<C1<<' '<<D1<<' '<<E1<<' '<<FIP<<' '<<net<<endl;
}
    
    

全部评论

相关推荐

粗心的雪碧不放弃:纯学历问题,我这几个月也是一直优化自己的简历,后来发现优化到我自己都觉得牛逼的时候,发现面试数量也没有提升,真就纯学历问题
点赞 评论 收藏
分享
整顿职场的柯基很威猛:这种不可怕,最可怕的是夹在一帮名校里的二本选手,人家才是最稳的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务