当前位置:网站首页>牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
2022-07-31 15:58:00 【顧棟】
描述
请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。
所有的IP地址划分为 A,B,C,D,E五类
A类地址从1.0.0.0到126.255.255.255;
B类地址从128.0.0.0到191.255.255.255;
C类地址从192.0.0.0到223.255.255.255;
D类地址从224.0.0.0到239.255.255.255;
E类地址从240.0.0.0到255.255.255.255
私网IP范围是:
从10.0.0.0到10.255.255.255
从172.16.0.0到172.31.255.255
从192.168.0.0到192.168.255.255
子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码)
(注意二进制下全是1或者全是0均为非法子网掩码)
注意:
- 类似于
【0.*.*.*】
和【127.*.*.*】
的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时请忽略 - 私有IP地址和A,B,C,D,E类地址是不冲突的
输入描述:
多行字符串。每行一个IP地址和掩码,用~隔开。
请参考帖子https://www.nowcoder.com/discuss/276处理循环输入的问题。
输出描述:
统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。
示例1
输入:
10.70.44.68~255.254.255.0
1.0.0.1~255.0.0.0
192.168.0.2~255.255.255.0
19..0.~255.255.255.0
输出:
1 0 1 0 0 2 1
说明:
10.70.44.68~255.254.255.0的子网掩码非法,19..0.~255.255.255.0的IP地址非法,所以错误IP地址或错误掩码的计数为2;
1.0.0.1~255.0.0.0是无误的A类地址;
192.168.0.2~255.255.255.0是无误的C类地址且是私有IP;
所以最终的结果为1 0 1 0 0 2 1
示例2
输入:
0.201.56.50~255.255.111.255
127.201.56.50~255.255.111.255
复制
输出:
0 0 0 0 0 0 0
复制
说明:
类似于【0.*.*.*】和【127.*.*.*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时请忽略
java 实现
- 主要考核IP和子网掩码的验证
package nowcoder.x1x;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HJ018 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int a = 0, b = 0, c = 0, d = 0, e = 0, err = 0, pri = 0;
String line;
while (null != (line = reader.readLine())) {
String[] ipAndMask = line.split("~");
String[] ips = ipAndMask[0].split("\\.");
// 过滤[0.*.*.*]和[127.*.*.*]
if ("0".equals(ips[0]) || "127".equals(ips[0])) {
continue;
}
// 验证子网掩码合法性
if (!isValidMask(ipAndMask[1])) {
err++;
System.out.println(ipAndMask[1]);//输出错误掩码
} else {
// 验证IP合法性
if (!isValidIp(ipAndMask[0])) {
err++;
System.out.println(ipAndMask[0]);//输出错误掩码
} else {
// A类地址
if (Integer.parseInt(ips[0]) >= 1 && Integer.parseInt(ips[0]) <= 126) {
if (Integer.parseInt(ips[0]) == 10) {
pri++;
a++;
} else {
a++;
}
}
// B类地址
if (Integer.parseInt(ips[0]) >= 128 && Integer.parseInt(ips[0]) <= 191) {
if (Integer.parseInt(ips[0]) == 172
&& (Integer.parseInt(ips[1]) >= 16
&& Integer.parseInt(ips[1]) <= 31)) {
pri++;
b++;
} else {
b++;
}
}
// C类地址
if (Integer.parseInt(ips[0]) >= 192 && Integer.parseInt(ips[0]) <= 223) {
if (Integer.parseInt(ips[0]) == 192
&& (Integer.parseInt(ips[1]) == 168)) {
pri++;
c++;
} else {
c++;
}
}
// D类地址
if (Integer.parseInt(ips[0]) >= 224 && Integer.parseInt(ips[0]) <= 239) {
d++;
}
// E类地址
if (Integer.parseInt(ips[0]) >= 240 && Integer.parseInt(ips[0]) <= 255) {
e++;
}
}
}
}
System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + err + " " + pri);
}
/** * 验证子网掩码 */
public static boolean isValidMask(String mask) {
if (!isValidIp(mask)) {
return false;
}
String[] maskTable = mask.split("\\.");
StringBuilder sb = new StringBuilder();
// 将mask转为32位二进制字符串
for (int i = 0; i < maskTable.length; i++) {
maskTable[i] = Integer.toBinaryString(Integer.parseInt(maskTable[i]));
if (maskTable[i].length() < 8) {
//不足8位补齐0
for (int j = 0; j < 8 - maskTable[i].length(); j++) {
sb.append("0");
}
}
sb.append(maskTable[i]);
}
// 最后一个1在第一个0之前,有效,否则无效
return sb.toString().lastIndexOf("1") < sb.toString().indexOf("0");
}
/** * 检验IP */
public static boolean isValidIp(String ip) {
String[] ipTable = ip.split("\\.");
if (ipTable.length != 4) {
return false;
}
for (String s : ipTable) {
if ("".equals(s) || Integer.parseInt(s) < 0 || Integer.parseInt(s) > 255) {
return false;
}
}
return true;
}
}
边栏推荐
猜你喜欢
"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)
对话庄表伟:开源第一课
[MySQL] Mysql paradigm and the role of foreign keys
button控件的使用
SringMVC中个常见的几个问题
How Redis handles concurrent access
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
基于ABP实现DDD
Kubernetes常用命令
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
随机推荐
Grafana安装后web打开报错
radiobutton的使用
Insert into data table to insert data
mysql黑窗口~建库建表
The normal form of the database (first normal form, second normal form, third normal form, BCNF normal form) "recommended collection"
Website vulnerability repair service provider's analysis of unauthorized vulnerability
全新宝马3系上市,安全、舒适一个不落
字符指针赋值[通俗易懂]
leetcode303 Weekly Match Replay
Linux查看redis版本(查看mongodb版本)
小程序:matlab解微分方程「建议收藏」
OPPO在FaaS领域的探索与思考
What is the difference between BI software in the domestic market?
网站漏洞修复服务商关于越权漏洞分析
The new BMW 3 Series is on the market, with safety and comfort
Bilateral filtering acceleration "recommended collection"
6-22 Vulnerability exploit - postgresql database password cracking
Delete table data or clear table
国内市场上的BI软件,到底有啥区别
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发