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

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

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

#include <iostream>
#include <istream>
#include <sstream>
#include <string>
#include<bits/stdc++.h>
#include <regex>
#include<cstring>
#include <bitset>
#include <stdlib.h>
#include <vector>
using namespace std;
bool judge_ip(string ip)
{
    string n;
    int number;
    istringstream ss(ip);
    int i=0;
    while(getline(ss,n,'.'))
    {
        i++;
        if(n.empty()) return false;
        number=stoi(n);
        if(number>255||number<0) return false;
    }
    if(i!=4) return false;
    return true;
}

bool judge_mask(string mask)
{
    string n;
    unsigned number=0;
    istringstream ss(mask);
    int i=0;
    while(getline(ss,n,'.'))
    {
        i++;
        if(n.empty()) return false;
        number=(number<<8)+stoi(n);
    }
    if(!number) return false;
    number=~number+1;
    if(number==1) return false;
    if(number&(number-1)) return false;
    if(i!=4) return false;
    return true;
}

bool judge_private(string ip)
{
    string n;
    int number;
    istringstream ss(ip);
    int i=0;
    vector<int> v(4,0);
    while(getline(ss,n,'.'))
    {
        if(n.empty()) return false;
        number=stoi(n);
        if(number>255||number<0) return false;
        v[i]=number;
        i++;
    }
    if(i!=4) return false;
    if(v[0]==10) return true;
    else if(v[0]==172&&v[1]>=16&&v[1]<=31) return true;
    else if(v[0]==192&&v[1]==168) return true;
    return false;
}
int main() {
    string line;
    string ip,mask;
    int a=0,b=0,c=0,d=0,e=0;
    int err=0,pri=0;
    while(getline(cin,line))
    {
        int pos=line.find('~');
        ip=line.substr(0,pos);
        mask=line.substr(pos+1,line.size());
        string head=ip.substr(0,ip.find('.'));
        if(stoi(head)==0||stoi(head)==127) ;
        else if(!judge_mask(mask)) err++;
        else if(!judge_ip(ip)) err++;
        else 
        {
        if(judge_private(ip)) pri++;
        if(stoi(head)>=1&&stoi(head)<=126) a++;
        else if(stoi(head)>=128&&stoi(head)<=191) b++;
        else if(stoi(head)>=192&&stoi(head)<=223) c++;
        else if(stoi(head)>=224&&stoi(head)<=239) d++;
        else if(stoi(head)>=240&&stoi(head)<=255) e++;
        }
    }
    cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<err<<" "<<pri;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

老板加个卤鸡蛋:HR看了以为来卧底来了
点赞 评论 收藏
分享
在笔试的大西瓜很矫健:校招数分不用想了,这经历和学历都不够用,大厂更别想,初筛都过不了,说点不好听的小厂数分都进不去(小厂也是假数分),要两个对口实习+3个项目(或者3+2),而且要有含金量才能补一点你的学历劣势。 建议刷实习,社招找数分,校招看运气,能入行业就行,可以运营转数分
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
正在热议
更多
# 长得好看会提高面试通过率吗? #
3637次浏览 45人参与
# 离家近房租贵VS离家远但房租低,怎么选 #
16869次浏览 137人参与
# 米连集团26产品管培生项目 #
7232次浏览 226人参与
# 春招至今,你的战绩如何? #
15402次浏览 142人参与
# 你的实习产出是真实的还是包装的? #
2934次浏览 52人参与
# 沪漂/北漂你觉得哪个更苦? #
1413次浏览 40人参与
# 巨人网络春招 #
11519次浏览 224人参与
# HR最不可信的一句话是__ #
1066次浏览 32人参与
# AI面会问哪些问题? #
926次浏览 22人参与
# 你做过最难的笔试是哪家公司 #
1201次浏览 21人参与
# AI时代,哪个岗位还有“活路” #
2781次浏览 51人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152881次浏览 889人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7990次浏览 43人参与
# XX请雇我工作 #
51154次浏览 171人参与
# 简历第一个项目做什么 #
32120次浏览 359人参与
# 简历中的项目经历要怎么写? #
310994次浏览 4262人参与
# 投格力的你,拿到offer了吗? #
178311次浏览 891人参与
# 你最满意的offer薪资是哪家公司? #
76961次浏览 375人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187576次浏览 1123人参与
# AI时代,哪些岗位最容易被淘汰 #
64632次浏览 871人参与
# 如果重来一次你还会读研吗 #
230002次浏览 2011人参与
# 正在春招的你,也参与了去年秋招吗? #
364304次浏览 2641人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务