当前位置: 首页 > news >正文

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

学习要点

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

题目链接

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

题目描述

解法

#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);// 先检查ipint 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或全为1W++;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后连续0W++;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;
}
http://www.lryc.cn/news/585854.html

相关文章:

  • 华为IPD(集成产品开发)流程是其研发管理的核心体系
  • Edge浏览器:报告不安全的站点的解决方案
  • 用YOLOv5系列教程(1)-用YOLOv5轻松实现设备状态智能监控!工业级教程来了
  • (C++)STL标准库(vector动态数组)(list列表)(set集合)(map键值对)相关对比,基础教程
  • 【Lucene/Elasticsearch】**Query Rewrite** 机制
  • U盘直接拔出不在电脑上弹出有何影响
  • 张量拼接操作
  • 文件上传漏洞2-常规厂商检测限制绕过原理讲解
  • 【学习笔记】Nginx常用安全配置
  • 新型深度神经网络架构:ENet模型
  • 零基础搭建监控系统:Grafana+InfluxDB 保姆级教程,5分钟可视化服务器性能!​
  • 《通信原理》学习笔记——第一章
  • PID控制算法理论学习基础——单级PID控制
  • houdini vat 学习笔记
  • LangChain 代理(Agents)学习
  • 《Java Web程序设计》实验报告五 Java Script学习汇报
  • dubbo源码学习3-dubbo反射调用服务源码分析
  • Leetcode百题斩-二分搜索
  • 【Linux仓库】虚拟地址空间【进程·陆】
  • 【学习笔记】Linux命令
  • AI:机器人未来的形态是什么?
  • 咨询导览,AI发展趋势
  • Leet code 每日一题
  • 【设计模式】外观模式(门面模式)
  • 飞算 JavaAI 智能编程助手:颠覆编程旧模式,重构新生态
  • ubuntu18.04 升级Ubuntu 20.04
  • vue3 el-table动态表头
  • Vue 项目打包部署还存在问题?你知道怎么做吧?
  • React - css 模块化(modules)
  • 解决‘vue‘ 不是内部或外部命令,也不是可运行的程序