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

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

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

第8/10组用例无法通过,代码没问题,是用例的问题。分析如下:

225.240.129.203—255.110.255.255----0 0 0 0 0 1 0

183.181.49.4—255.0.0.0----0 1 0 0 0 1 0

172.177.113.45—255.0.0.0----0 2 0 0 0 1 0

176.134.46.246—255.0.0.0----0 3 0 0 0 1 0

153.63.21.56—255.255.58.255----0 3 0 0 0 2 0

23.135.167.228—255.0.0.0----1 3 0 0 0 2 0

204.58.47.149—255.0.0.0----1 3 1 0 0 2 0

113.33.181.46—255.255.255.0----2 3 1 0 0 2 0

73.245.52.119—255.255.154.0----2 3 1 0 0 3 0

23.214.47.71—255.0.0.0----3 3 1 0 0 3 0

139.124.188.91—255.255.255.100----3 3 1 0 0 4 0

142.94.192.197—255.0.0.0----3 4 1 0 0 4 0

53.173.252.202—255.0.0.0----4 4 1 0 0 4 0

127.201.56.50—255.255.111.255----4 4 1 0 0 5 0

118.251.84.111—255.0.0.0----5 4 1 0 0 5 0

130.27.73.170—255.0.0.0----5 5 1 0 0 5 0

253.237.54.56—255.86.0.0----5 5 1 0 0 6 0

64.189.222.111—255.255.255.139----5 5 1 0 0 7 0

148.77.44.147—255.0.0.0----5 6 1 0 0 7 0

59.213.5.253—255.255.0.0----6 6 1 0 0 7 0

3.52.119.131—255.255.0.0----7 6 1 0 0 7 0

213.208.164.145—255.255.0.0----7 6 2 0 0 7 0

24.22.21.206—255.255.90.255----7 6 2 0 0 8 0

89.43.34.31—255.0.0.0----8 6 2 0 0 8 0

9.64.214.75—255.0.0.0----9 6 2 0 0 8 0

110.156.20.173—255.153.0.0----9 6 2 0 0 9 0

71.183.242.53—255.255.0.0----10 6 2 0 0 9 0

119.152.129.100—255.0.0.0----11 6 2 0 0 9 0

38.187.119.201—255.0.0.0----12 6 2 0 0 9 0

73.81.221.180—255.255.255.255----12 6 2 0 0 10 0

73.198.13.199—255.0.15.0----12 6 2 0 0 11 0

99.42.142.145—255.255.255.0----13 6 2 0 0 11 0

196.121.115.160—255.0.0.0----13 6 3 0 0 11 0

226.30.29.206—255.0.0.0----13 6 3 1 0 11 0

244.248.31.171—255.255.255.255----13 6 3 1 0 12 0

59.116.159.246—255.0.0.0----14 6 3 1 0 12 0

121.124.37.157—255.0.0.226----14 6 3 1 0 13 0

103.42.94.71—255.255.0.0----15 6 3 1 0 13 0

125.88.217.249—255.255.74.255----15 6 3 1 0 14 0

73.44.250.101—255.255.255.0----16 6 3 1 0 14 0

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

int isValidIP(vector<int>& vec) {
    if (vec[0] == 0 || vec[0] == 127) {
        return 0;
    }
    if (vec.size() == 4) {
        //A
        if (vec[0] >= 1 && vec[0] <= 126) {
            int cnt = 0;
            for (int i = 1; i < 4; ++i) {
                if (vec[i] >= 0 && vec[i] <= 255) {
                    ++cnt;
                }
            }
            if (cnt == 3) {
                return 1;
            }
        }
        //B
        else if (vec[0] >= 128 && vec[0] <= 191) {
            int cnt = 0;
            for (int i = 1; i < 4; ++i) {
                if (vec[i] >= 0 && vec[i] <= 255) {
                    ++cnt;
                }
            }
            if (cnt == 3) {
                return 2;
            }
        }
        //C
        else if (vec[0] >= 192 && vec[0] <= 223) {
            int cnt = 0;
            for (int i = 1; i < 4; ++i) {
                if (vec[i] >= 0 && vec[i] <= 255) {
                    ++cnt;
                }
            }
            if (cnt == 3) {
                return 3;
            }
        }
        //D
        else if (vec[0] >= 224 && vec[0] <= 239) {
            int cnt = 0;
            for (int i = 1; i < 4; ++i) {
                if (vec[i] >= 0 && vec[i] <= 255) {
                    ++cnt;
                }
            }
            if (cnt == 3) {
                return 4;
            }
        }
        //E
        else if (vec[0] >= 240 && vec[0] <= 255) {
            int cnt = 0;
            for (int i = 1; i < 4; ++i) {
                if (vec[i] >= 0 && vec[i] <= 255) {
                    ++cnt;
                }
            }
            if (cnt == 3) {
                return 5;
            }
        }
    }
    //Error IP
    return 6;
}

bool isPrivateIP(vector<int>& vec, int IPClass) {
    if (IPClass == 1 && vec[0] == 10) {
        return true;
    } else if (IPClass == 2 && vec[0] == 172) {
        if (vec[1] >= 16 && vec[1] <= 31) {
            return true;
        }
    } else if (IPClass == 3 && vec[0] == 192 && vec[1] == 168) {
        return true;
    }

    return false;
}

bool isValidMask(vector<int>& vec, int maskV) {
    if (maskV == 0 || maskV == 6) {
        return false;
    }
    unsigned int value = vec[3] + vec[2] * pow(2, 8) + vec[1] * pow(2,
                         16) + vec[0] * pow(2,
                                            24);
    if (value == 0 || value == 4294967295) {
        return false;
    }
    unsigned int temp = ~value + 1;
    int cnt = 0;
    while (temp) {
        if (temp & 1) {
            ++cnt;
        }
        temp >>= 1;
    }
    if (cnt == 1) {
        return true;
    }
    return false;
}


void processStr(string& str, vector<int>& data) {
    for (int i = 0; i < str.size(); ++i) {
        int start = i;
        while (str[i] != '.') {
            ++i;
        }
        int end = i - 1;
        if (end >= start) {
            data.push_back(stoi(str.substr(start, end - start + 1)));
        }
    }
}

int main(int argc, char* argv[]) {
    string str;
    string strIP;
    string strMask;
    vector<int> res(8, 0);
    while (getline(cin, str)) {
        int gap = 0;
        while (str[gap] != '~') {
            ++gap;
        }
        strIP = str.substr(0, gap);
        strMask = str.substr(gap + 1, str.size() - gap - 1);
        vector<int> vecMask;
        processStr(strMask, vecMask);
        int maskV = isValidIP(vecMask);
        if (!isValidMask(vecMask, maskV)) {
            res[6] += 1;
        } else {
            vector<int> vecIP;
            processStr(strIP, vecIP);
            int IPClass = isValidIP(vecIP);
            if (IPClass) {
                res[IPClass] += 1;
                if (IPClass <= 3 && isPrivateIP(vecIP, IPClass)) {
                    res[7] += 1;
                }
            }
        }
    }
    for (int i = 1; i < 8; ++i) {
        cout << res[i] << " ";
    }
    return 0;
}
全部评论

相关推荐

11-15 17:19
湖南大学 Java
成果成果成果果:这是哪个公司的hr,这么离谱吗,我没见过用性别卡技术岗的,身边女性同学拿大厂offer的比比皆是
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务