题解 | #识别有效的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;
}
    
    

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-27 10:52
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 12:19
点赞 评论 收藏
分享
hso_:哈哈哈哈哈哈我没offer一样在同一道题开喷了
投递深圳同为数码等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务