题解 | #判断两个IP是否属于同一子网#

判断两个IP是否属于同一子网

http://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218

#include <bits/stdc++.h>

using namespace std;

string str2bin(int num){
    if(num == 0) return "00000000";
    
    //cout << "num = " << num << endl;
    string binRes = "";
    string help = "0123456789ABCDEF";
    while(num){
        binRes += help[num % 2]; //二进制
        num /= 2;
    }
    reverse(binRes.begin(), binRes.end());
    while(binRes.size() < 8){
        binRes.insert(binRes.begin(), '0');
    } 
    //cout << "binRes = " << binRes << endl;
    return binRes;
}

bool noLegalYanMa(string zwYanMa){
    string res = "";
    stringstream iss(zwYanMa);
    string tmp = "";
    bool isNeg = false;
    while(getline(iss, tmp, '.')){       
        if(tmp[0] == '-'){
            isNeg = true;
            tmp = tmp.substr(1, tmp.size() - 1);
        } 
        //cout << "tmp = " << tmp << endl;
        int size = tmp.size();
        int num = 0;
        for(int i = 0; i < size; i++){
            num = num * 10 + (tmp[i] - '0');
        }
        if(isNeg) num = -num;
        
        //cout << "num==" << num << endl;
        if(num < 0 || num > 255) return true;
        
        res += str2bin(num);
    }
    //cout << "res = " << res << endl;
    bool notZero = true;
    for(int j = 0; j < res.size(); j++){
        if(res[j] == '0') {
            notZero = false;
        }
        else{
            if(!notZero){
                return true;
            }
            continue;
        }   
    }
    
    return false;
}

string str2binStr(string s){
    string res = "";
    stringstream iss(s);
    string tmp = "";
    while(getline(iss, tmp, '.')){
        //cout << "tmp = " << tmp << endl;       
        int size = tmp.size();
        int num = 0;
        for(int i = 0; i < size; i++){
            num = num * 10 + (tmp[i] - '0');
        }
        //cout << "num==" << num << endl;       
        res += str2bin(num);
    }
    
    return res;
}

bool noLegalIP(string IP){
    string res = "";
    stringstream iss(IP);
    string tmp = "";
    bool isNeg = false;
    while(getline(iss, tmp, '.')){
        if(tmp[0] == '-'){
            isNeg = true;
            tmp = tmp.substr(1, tmp.size() - 1);
        } 
        //cout << "tmp = " << tmp << endl;
        int size = tmp.size();
        int num = 0;
        for(int i = 0; i < size; i++){
            num = num * 10 + (tmp[i] - '0');
        }
        if(isNeg) num = -num;
        //cout << "num==" << num << endl; 
        if(num < 0 || num > 255) return true;
    }
    
    return false;
}

string anWeiSum(string zwYanMa, string IP){
    string s1 = str2binStr(zwYanMa);
    string s2 = str2binStr(IP);
    cout << s1 << endl;
    cout << s2 << endl;
    
    string res = "";
    int i = s1.size() - 1;
    int j = s2.size() - 1;
    int carry = 0; //进位
    while(i >= 0 || j >= 0){
        int digitA = i >= 0 ? (s1[i--] - '0') : 0; //i--
        int digitB = j >= 0 ? (s2[j--] - '0') : 0; //j--
        int sum = digitA + digitB + carry;
        carry = sum >= 2 ? 1 : 0;
        sum = sum >= 2 ? (sum - 2) : sum;
        
        res += to_string(sum);
    }
    if(carry == 1) res += "1";
    reverse(res.begin(), res.end());
    cout << res << endl;
    return res;
}

string anWeiYu(string zwYanMa, string IP){
    string s1 = str2binStr(zwYanMa);
    string s2 = str2binStr(IP);
    //cout << s1 << endl;
    //cout << s2 << endl;
    
    string res;
    for(int i = 0; i < 32; i++){
        if(s1[i] != s2[i]){
            res += "0";
        }
        else if(s1[i] == '0' && s2[i] == '0'){
            res += "0";
        }
        else{ //s1[i] == '1' && s2[i] == '1'
            res += "1";
        }
    }
    //cout << res << endl;
    return res;
}

bool isSameZiWang(string zwYanMa, string firstIP, string secondIP){
    if(anWeiYu(zwYanMa, firstIP) == anWeiYu(zwYanMa, secondIP)){
        return true;
    }
    return false;
}

void process(string zwYanMa, string firstIP, string secondIP, int& res){
    if(noLegalYanMa(zwYanMa) || noLegalIP(firstIP) || noLegalIP(secondIP)){
        res = 1;
        return;
    }

    if(isSameZiWang(zwYanMa, firstIP, secondIP)){
        res = 0;
    }
    else{
        res = 2;
    }
    
    return;
}

int main(){
    string zwYanMa = "";
    string firstIP = "";
    string secondIP = "";
    getline(cin, zwYanMa);
    getline(cin, firstIP);
    getline(cin, secondIP);
    int res = 0;

    process(zwYanMa, firstIP, secondIP, res);
    //anWeiYu(zwYanMa, firstIP);
    cout << res << endl;
    return 0;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

04-16 10:27
已编辑
美团_Saas_后端开发
今天周一休息,突发奇想写一篇阶段总结。如题,我已经去了一个和Java彻底毫无关联的行业。曾经我以为自己能在计算机行业发光发热,拿到美团offer那会感觉自己天都亮了。没想到刚入行一年多就当了逃兵。从最开始的热爱到现在一看到代码就厌恶,不知道自己经历了什么。所以我去干什么了?答案是:在成都当了租房销售。上班那会压力大了就念叨着去干租房中介,但是一直下不去这个决心,想着自己学了四年多的计算机知识,终究还是不甘心。终于在某一天准备八股文的时候,看着无数篇和工作内容关系不大的理论知识,那一刻下定决心,决定尝试一下销售行业,也算是给自己一个交代。后面阴差阳错的投了成都自如去当租房管家,没想到面试很顺利,在当天一百多个面试的人里面,我成为了为数不多通过的几个幸运儿之一。目前已经培训通过,正式入职,也开了单,有压力但是每天过得很开心,真心喜欢那种和人交流的感觉,哪怕是最后没有选择找我租房。说这些也是想告诉那些大三,大四正在找Java实习而焦虑的同学:你们现在还年轻,选择很多,容错率也很高,可以尽情去尝试自己喜欢的行业和工作。不用因为某一次的面试没通过或者简历石沉大海而焦虑,更不用因为身边人都在挤编程的独木桥就强迫自己跟风。也算是自己的碎碎念吧,也希望自己能在新的领域取得一点小成就。也祝牛油工作顺利!
沉淀小子:干啥都不丢人啊,生存是必须要的,销售很考验一个人综合素质能力的,好的销售人脉和资源可不比写字楼的白领差啊
点赞 评论 收藏
分享
讲原则的小黄鸭不愿吃...:有时候面试眼缘确实很重要,当然,飞驰人生2中张弛说的很对:我努力了无数次,但是我知道机会只会出现在其中一两次。你把每一次笔试面试都全力以赴,总有你运气发挥到位的时候
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务