leetcode:HJ18 识别有效的IP地址和掩码并进行分类统计[华为机考][字符串]

学习要点

  1. bitset<8>
  2. ostringstream
  3. stoi
  4. string.find
  5. string.substr

题目链接

识别有效的IP地址和掩码并进行分类统计_牛客题霸_牛客网

题目描述

解法

cpp 复制代码
#include <iostream>
#include <bits/stdc++.h>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main() {
    int A = 0;int B = 0;int C = 0;int D = 0;int E = 0;int W = 0;int P = 0;  
    string line_str;
    while (getline(cin, line_str)) {
        string ip;  string str1; string str2; string str3; string str4;
        string net;
        ip = line_str.substr(0, line_str.find('~'));
        net = line_str.substr(line_str.find('~') + 1);
        // 先检查ip
        int point1 = ip.find('.');
        int point2 = ip.find('.',point1+1);
        int point3 = ip.find('.',point2+1);
        if((point1 == 0) || ( point1+1 == point2) || (point2+1 == point3) || (point3 == ip.size() -1))
        {                   // 检查ip是否合法
            W++;
            continue;
        }
        str1 = ip.substr(0,point1);
        str2 = ip.substr(point1+1,point2-point1-1);
        if(str1 == "0" || str1 == "127")
        {                   // 检查是否是特殊地址
            continue;
        }

        // 再检查子网掩码
        int point_1 = net.find('.');
        int point_2 = net.find('.',point_1+1);
        int point_3 = net.find('.',point_2+1);
        if((point_1 == 0) || ( point_1+1 == point_2) || (point_2+1 == point_3) || (point_3 == net.size() -1))
        {       // 检查子网掩码是否合法---点号
            W++;
            continue;
        }
        int num1 = stoi(net.substr(0,point_1));
        int num2 = stoi(net.substr(point_1+1,point_2 - point_1 - 1));
        int num3 = stoi(net.substr(point_2+1,point_3 - point_2 - 1));
        int num4 = stoi(net.substr(point_3+1));
        ostringstream outstr;
        bitset<8> binary1(num1); bitset<8> binary2(num2); bitset<8> binary3(num3); bitset<8> binary4(num4); 
        outstr << binary1 << binary2 << binary3 << binary4;
        string binary_net = outstr.str();
        int zero_pos = binary_net.find('0');
        if(zero_pos == string::npos || binary_net.find('1') == string::npos ) 
        {       // 检查子网掩码是否合法---全为0或全为1
            W++;
            continue;
        }
        bool flag = false;
        for(int i = zero_pos+1; i<binary_net.size();i++)
        {
            if(binary_net[i] == '1')
            {
                flag = true;
            }
        }
        if(flag)
        {       // 检查子网掩码是否合法---连续1后连续0
            W++;
            continue;
        }

        // 至此全部合法且不为特殊地址
        if(str1 == "10" || (str1 == "172" && stoi(str2) >= 16 && stoi(str2)<= 31) || (str1 == "192" && str2 == "168"))
        {
            P++;
        }

        if(stoi(str1) >= 1 && stoi(str1) <= 127)
        {
            A++;continue;
        }
        if(stoi(str1) >= 128 && stoi(str1) <= 191)
        {
            B++;continue;
        }
        if(stoi(str1) >= 192 && stoi(str1) <= 223)
        {
            C++;continue;
        }
        if(stoi(str1) >= 224 && stoi(str1) <= 239)
        {
            D++;continue;
        }
        if(stoi(str1) >= 240 && stoi(str1) <= 255)
        {
            E++;continue;
        }
    }
    cout << A << ' ' << B << ' ' << C << ' ' << D << ' ' << E << ' ' << W << ' ' << P << endl;
}
相关推荐
Q741_1477 分钟前
每日一题 力扣 3655. 区间乘法查询后的异或 II 模拟 分治 乘法差分法 快速幂 C++ 题解
c++·算法·leetcode·模拟·快速幂·分治·差分法
The_Ticker8 分钟前
印度股票实时行情API(低成本方案)
python·websocket·算法·金融·区块链
夏乌_Wx12 分钟前
剑指offer | 2.4数据结构相关题目
数据结构·c++·算法·剑指offer·c/c++
浮芷.33 分钟前
Flutter 框架跨平台鸿蒙开发 - 家庭健康监测云应用
科技·flutter·华为·harmonyos·鸿蒙
世人万千丶1 小时前
Flutter 框架跨平台鸿蒙开发 - 宠物语言翻译器应用
学习·flutter·华为·开源·harmonyos·鸿蒙
AI成长日志1 小时前
【笔面试算法学习专栏】哈希表基础:两数之和与字母异位词分组
学习·算法·面试
见山是山-见水是水1 小时前
Flutter 框架跨平台鸿蒙开发 - NPC模拟器
flutter·华为·harmonyos
abant21 小时前
leetcode 239 单调队列 需要一些记忆
算法·leetcode·职场和发展
提子拌饭1331 小时前
番茄时间管理:鸿蒙Flutter 实现的高效时间管理工具
android·flutter·华为·架构·开源·harmonyos·鸿蒙
漫霂1 小时前
二叉树的统一迭代遍历
java·算法