当前位置:网站首页>牛客 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;
}
}
边栏推荐
- form 表单提交后,使页面不跳转[通俗易懂]
- Website vulnerability repair service provider's analysis of unauthorized vulnerability
- Tencent Cloud Deployment----DevOps
- 更新数据表update
- Vb how to connect mysql_vb how to connect to the database collection "advice"
- 在资源管理类中提供对原始资源的访问——条款15
- Character pointer assignment [easy to understand]
- [MySQL] Mysql paradigm and the role of foreign keys
- 做事软件开发-法的重要性所在以及合理结论的认识
- Summary of the implementation method of string inversion "recommended collection"
猜你喜欢

The 2nd China PWA Developer Day

Dialogue with Zhuang Biaowei: The first lesson of open source

The use of border controls

MySQL基础篇【单行函数】

使用 GraphiQL 可视化 GraphQL 架构

11 pinia使用

How to switch remote server in gerrit

tooltips使用教程(鼠标悬停时显示提示)

C language "the third is" upgrade (mode selection + AI chess)
![[TypeScript] In-depth study of TypeScript type operations](/img/d9/ee240ccba72e8d3114ee5c52ed0c8f.png)
[TypeScript] In-depth study of TypeScript type operations
随机推荐
After Grafana is installed, the web opens and reports an error
arm按键控制led灯闪烁(嵌入式按键实验报告)
Qt practical cases (54) - using transparency QPixmap design pictures
Bilateral filtering acceleration "recommended collection"
11 pinia使用
基于ABP实现DDD
多主复制下处理写冲突(4)-多主复制拓扑
01 Encounter typescript, build environment
C语言”三子棋“升级版(模式选择+AI下棋)
type of timer
Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
6-22漏洞利用-postgresql数据库密码破解
Implementing click on the 3D model in RenderTexture in Unity
Why is the field of hacking almost filled with boys?
ML.NET related resources
JVM parameter analysis Xmx, Xms, Xmn, NewRatio, SurvivorRatio, PermSize, PrintGC "recommended collection"
Stuck in sill idealTree buildDeps during npm installation, npm installation is slow, npm installation is stuck in one place
MySQL的相关问题
Premiere Pro 2022 for (pr 2022)v22.5.0
01 邂逅typescript,环境搭建