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

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

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

#include <iostream>
#include <cctype>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
bool isValidIPAddress(const string &ip)
{
    istringstream iss(ip);
    string segment;
    unsigned count = 0;
    while (getline(iss, segment, '.'))
    {
        if (segment.empty() || segment.size() > 3)
        {
            return false;
        }

        for (char ch : segment)
        {
            if (!isdigit(ch))
            {
                return false;
            }
        }

        int num = stoi(segment);
        if (num < 0 || num > 255)
        {
            return false;
        }

        ++count;
    } 
    return count == 4;
}
bool is0or127segment(const string &ip)
{
    istringstream iss(ip);
    string segment;
    getline(iss, segment, '.');
    if (stoi(segment) == 0 || stoi(segment) == 127)
    {
        return true;
    }
    return false;
}
bool isValidSubnetMask(const string &mask)
{
    istringstream iss(mask);
    string segment;
    unsigned result = 0;
    while (getline(iss, segment, '.'))
    {
        result = (result << 8) + stoi(segment);
    }

    if (result == 0 || result == 0xFFFFFFFF)
    {
        return false;
    }

    return (result | result-1) == 0xFFFFFFFF ? true : false;
}
bool isPrivateIPAddress(const string &ip)
{
    istringstream iss(ip);
    string segment;
    vector<int> v;
    while (getline(iss, segment, '.'))
    {
        v.push_back(stoi(segment));
    }
    if (v[0] == 10)
    {
        return true;
    }
    if (v[0] == 172 && (v[1] >= 16 && v[1] <= 31))
    {
        return true;
    }
    if (v[0] == 192 && v[1] == 168)
    {
        return true;
    }

    return false;
}

int main()
{
    string record;
    unsigned a=0, b=0, c=0, d=0, e=0, err=0, p=0;
    while (cin >> record)
    {
        istringstream iss(record);
        string split;
        vector<string> v;
        while (getline(iss, split, '~'))
        {
            v.push_back(split);
        }
        if (is0or127segment(v[0]))
        {
            continue;
        }
        if (!isValidIPAddress(v[1]) || !isValidSubnetMask(v[1]))
        {
            ++err;
        }
        else if (!isValidIPAddress(v[0]))
        {
            
            ++err;
        }
        else
        {
            if (isPrivateIPAddress(v[0]))
            {
                ++p;
            }
            int firseg = stoi(v[0].substr(0, v[0].find_first_of('.')));
            if (firseg > 0 && firseg < 127)
            {
                ++a;
            }
            else if (firseg > 127 && firseg < 192)
            {
                ++b;
            }
            else if (firseg > 191 && firseg < 224)
            {
                ++c;
            }
            else if (firseg > 223 && firseg < 240)
            {
                ++d;
            }
            else if (firseg > 239 && firseg < 256)
            {
                ++e;
            }
        }
    }
    cout<< a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' '
        << err << ' ' << p << endl; 
    return 0;
}

我想问一下判断子网掩码有效与否为什么别人的答案有(b & b-1) == 0,为什么不是我的63行这样

(result | result-1) == 0xFFFFFFFF,我举个例子b=1100,b-1 =1011 ,第一个式子得到1000 不等于0,所以1100是无效掩码?

如果用我的判断,等于1111,所以是有效掩码,符合假设。所以用 &的能跑通这个题是为什么?

更新:原来他们的答案有取反~操作,是我看漏了,那应该对了

全部评论

相关推荐

不愿透露姓名的神秘牛友
08-07 14:45
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
球球别拷打俺了:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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