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

HJ39判断两个IP是否属于同一子网(中)

提示:文章

文章目录

  • 前言
  • 一、背景
  • 二、
    • 2.1
    • 2.2
  • 总结

前言

接上文HJ39判断两个IP是否属于同一子网


查了下,atoi可以转换负数。

修改成下面的代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>bool isTargetSonNet(int array[], int len)
{int tempArray[32] = {0};int tempArrayIndex = 0;for(int i = 0; i < len; i++){int data = array[i];if(data > 255 || data < 0){return false;}int index = ((tempArrayIndex + 1) * 8);while(data){int dat = data % 2;tempArray[--index] = dat;data /= 2;}tempArrayIndex++;}int count = 0;for(int i = 0; i + 1 < 32; i++){int temp = tempArray[i] - tempArray[i + 1];if(temp != 0 && temp != 1){return false;}if(temp == 1){count++;}}if(count != 1){return false;}return true;
}int main() {int a, b;char sonNet[20] = {'\0'};char ip1[20] = {'\0'};char ip2[20] = {'\0'};int arraySonNet[4] = {0};int arrayIp1[4] = {0};int arrayIp2[4] = {0};while (scanf("%s", sonNet) != EOF) { // 注意 while 处理多个 case// 64 位输出请用 printf("%lld") to char output = 'f';char delimiters[2] = ".";char* p = strtok(sonNet, delimiters);int index = 0;while(p != NULL){//printf("%s\n", p);int data = atoi(p);// if(data > 255)// {//     output = '1';//     break;// }// else if(data != 255 && data != 0)// {//     output = '1';//     break;// }arraySonNet[index++] = data;p = strtok(NULL, delimiters);}if(!isTargetSonNet(arraySonNet, 4)){output = '1';}if(output != 'f'){printf("%c\n", output);break;}if(scanf("%s", ip1) != EOF){p = strtok(ip1, delimiters);int index = 0;while(p != NULL){//printf("%s\n", p);int data = atoi(p);if(data > 255 || data < 0){output = '1';break;}arrayIp1[index++] = data;p = strtok(NULL, delimiters);}}if(output != 'f'){printf("%c\n", output);break;}if(scanf("%s", ip2) != EOF){p = strtok(ip2, delimiters);int index = 0;while(p != NULL){//printf("%s\n", p);int data = atoi(p);if(data > 255 || data < 0){output = '1';break;}arrayIp2[index++] = data;p = strtok(NULL, delimiters);}}if(output != 'f'){printf("%c\n", output);break;}int count = 0;for(int i = 0; i < 4; i++){if( (ip1[i] & sonNet[i]) == (ip2[i] & sonNet[i]) ){++count;}}if(output == 'f'){if(count == 4){output = '0';}else {output = '2';}}printf("%c\n", output);}return 0;
}

验证一下demo示例

示例有错误

255.255.255.0
192.168.224.256
192.168.10.4
255.0.0.0
193.194.202.15
232.43.7.59
255.255.255.0
192.168.0.254
192.168.0.1

输出 1

修改成下面的代码也不行

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>bool isTargetSonNet(int array[], int len)
{int tempArray[32] = {0};int tempArrayIndex = 0;for(int i = 0; i < len; i++){int data = array[i];if(data > 255 || data < 0){return false;}int index = ((tempArrayIndex + 1) * 8);while(data){int dat = data % 2;tempArray[--index] = dat;data /= 2;}tempArrayIndex++;}int count = 0;for(int i = 0; i + 1 < 32; i++){int temp = tempArray[i] - tempArray[i + 1];if(temp != 0 && temp != 1){return false;}if(temp == 1){count++;}}if(count != 1){return false;}return true;
}int main() {int a, b;char sonNet[20] = {'\0'};char ip1[20] = {'\0'};char ip2[20] = {'\0'};int arraySonNet[4] = {0};int arrayIp1[4] = {0};int arrayIp2[4] = {0};while (scanf("%s", sonNet) != EOF) { // 注意 while 处理多个 case// 64 位输出请用 printf("%lld") to char output = 'f';char delimiters[2] = ".";char* p = strtok(sonNet, delimiters);int index = 0;while(p != NULL){//printf("%s\n", p);int data = atoi(p);// if(data > 255)// {//     output = '1';//     break;// }// else if(data != 255 && data != 0)// {//     output = '1';//     break;// }arraySonNet[index++] = data;p = strtok(NULL, delimiters);}if(!isTargetSonNet(arraySonNet, 4)){output = '1';}if(output != 'f'){printf("%c\n", output);break;}if(scanf("%s", ip1) != EOF){p = strtok(ip1, delimiters);int index = 0;while(p != NULL){//printf("%s\n", p);int data = atoi(p);if(data > 255 || data < 0){output = '1';break;}arrayIp1[index++] = data;p = strtok(NULL, delimiters);}}if(output != 'f'){printf("%c\n", output);continue;}if(scanf("%s", ip2) != EOF){p = strtok(ip2, delimiters);int index = 0;while(p != NULL){//printf("%s\n", p);int data = atoi(p);if(data > 255 || data < 0){output = '1';break;}arrayIp2[index++] = data;p = strtok(NULL, delimiters);}}if(output != 'f'){printf("%c\n", output);continue;}int count = 0;for(int i = 0; i < 4; i++){if( (ip1[i] & sonNet[i]) == (ip2[i] & sonNet[i]) ){++count;}}if(output == 'f'){if(count == 4){output = '0';}else {output = '2';}}printf("%c\n", output);}return 0;
}

示例有错误

255.255.255.0
192.168.224.256
192.168.10.4
255.0.0.0
193.194.202.15
232.43.7.59
255.255.255.0
192.168.0.254
192.168.0.1

输出

1
1
2
0

修改代码,修改成goto,但是还是不行,会打印4个输出,很奇怪,我就调试。调试后我意识到goto会跳过其他scanf操作。
应该先把数据采集完在处理。


总结

接下文:HJ39判断两个IP是否属于同一子网(下)

相关文章:

  • java 线程之间通信-volatile 和 synchronized
  • MySQL版本发布模型
  • unity2d Ugui--Image城市道路汽车行驶
  • 多层级iframe下JS调用点击事件
  • Sui的Fastcrypto加密库刷新速度记录
  • 【单片机】三极管的电路符号及图片识别
  • 机动车检测站授权签字人精选试题(附答案)
  • elasticsearch的入门与实践
  • 入侵检测系统(IDS)
  • 【文末附gpt升级秘笈】关于“登月游戏”的详细内容介绍
  • java程序什么时候需要在运行的时候动态修改字节码对象
  • minSdkVersion、targetSdkVersion、compileSdkVersion三者的作用解析
  • [Qt] Qt Creator中配置 Vs-Code 编码风格
  • 算法第八天:leetcode 35.搜索插入位置
  • TVBOX 最新版下载+视频源教程
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • CSS 三角实现
  • Docker 笔记(2):Dockerfile
  • docker-consul
  • Fundebug计费标准解释:事件数是如何定义的?
  • PV统计优化设计
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • Terraform入门 - 3. 变更基础设施
  • ucore操作系统实验笔记 - 重新理解中断
  • 程序员该如何有效的找工作?
  • 经典排序算法及其 Java 实现
  • 盘点那些不知名却常用的 Git 操作
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 使用 @font-face
  • 在weex里面使用chart图表
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (C++二叉树05) 合并二叉树 二叉搜索树中的搜索 验证二叉搜索树
  • (ibm)Java 语言的 XPath API
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (二十三)Flask之高频面试点
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (十)T检验-第一部分
  • (轉貼) UML中文FAQ (OO) (UML)
  • *++p:p先自+,然后*p,最终为3 ++*p:先*p,即arr[0]=1,然后再++,最终为2 *p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .Net Core 微服务之Consul(二)-集群搭建
  • .NET Core使用NPOI导出复杂,美观的Excel详解
  • .NET Core中的时区转换问题
  • .NET NPOI导出Excel详解
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .Net Remoting常用部署结构
  • .NET 发展历程