IP Networks UVA - 1590
Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he decided to group all those IP addresses into the smallest possible IP network.
Each IP address is a 4-byte number that is written byte-by-byte in a decimal dot-separated notation “byte0.byte1.byte2.byte3” (quotes are added for clarity). Each byte is written as a decimal number from 0 to 255 (inclusive) without extra leading zeroes.
IP network is described by two 4-byte numbers — network address and network mask. Both network address and network mask are written in the same notation as IP addresses.
In order to understand the meaning of network address and network mask you have to consider their binary representation. Binary representation of IP address, network address, and network mask consists of 32 bits: 8 bits for byte0 (most significant to least significant), followed by 8 bits for byte1, followed by 8 bits for byte2, and followed by 8 bits for byte3.
IP network contains a range of 2n IP addresses where 0 ≤ n ≤ 32. Network mask always has 32−n first bits set to one, and n last bits set to zero in its binary representation. Network address has arbitrary 32 −n first bits, and n last bits set to zero in its binary representation. IP network contains all IP addresses whose 32 −n first bits are equal to 32 −n first bits of network address with arbitrary n last bits. We say that one IP network is smaller than the other IP network if it contains fewer IP addresses.
For example, IP network with network address 194.85.160.176 and network mask 255.255.255.248 contains 8 IP addresses from 194.85.160.176 to 194.85.160.183 (inclusive).
Input
The input file will contain several test cases, each of them as described below.
The first line of the input file contains a single integer number m (1 ≤m≤ 1000). The following m lines contain IP addresses, one address on a line. Each IP address may appear more than once in the input file.
Output
For each test case, write to the output file two lines that describe the smallest possible IP network that contains all IP addresses from the input file. Write network address on the first line and network mask on the second line.
Sample Input
3
194.85.160.177
194.85.160.183
194.85.160.178
Sample Output
194.85.160.176
255.255.255.248
HINT
题目的意思就是给你一堆网址,让你找出所有网址相同的部分和不相同的部分,然后求出不相同的部分的位数n(网址是由四个数每一个数占有8位),然后求出最小的网络地址,也就把求得的n位全部改成0,然后按照每八位一个数的方式打印出来,同样打印子网掩码也是将最后n位补上0,然后按照子网的方式打印。
解决思路:
这个题实际上暗示了让我们使用位运算,但是如何使用位运算?如何表示每一个数?如何求出n?
因为int型数字有32位,二每一个子网有四个数字然后每八个是一个数。不能直接用int值表示4个数,因此我们采取一个数字一个数字来表示的方法。
对于位运算,我们需要知道从哪里开始使用位运算,也就是判断给定的网址从哪里开始出现不同。这里采用的方式是对每一个网址的每一个数都放到一个数组的一维里面,然后对其进行排序,如果最后一个和第一个出现不一样那么就从这里开始位运算。
位运算是干什么的?
这里我们要判断n的大小,不能仅仅从十进制大小进行判断,例如8和9的二进制分别是1000和1001,他们只有最后一个位是不同的,前三位都是相同的。因此位运算就是来判断n的大小不仅仅从10进制数来判断。
那么,如何求出n的大小?
我们是将32位分成八位进行显示的,因为后n位的二进制值都是0所以如果实际的n是9位,那么最后八位都是0显示的十进制也是0,因此我们只需要将四位数中从左往右算第一个出现不同的数里面求出他们不一样的位数即可,剩下的全部为0。因为各个数前面的位都是相同的,只有后面的是不同的,采取位运算,令两个数字每次右移一个位知道两个数相同为止。比如1000和1001右移一位编程100和100是相同的,就求出来n是1。然后将这一个编码值先右移n位然后左移n位,后面n位就自动变成0。然后输出结果。
对于子网掩码大同小异。操作只需要和上面的位运算相同即可。
Accepted
#include<stdio.h>
#include<stdlib.h>
int bit(int a, int b) //求出n的位数
{
int i = 0; //计算n的位数
while (a != b)
{
a=a>>1; //a,b同时右移一位直到相等
b=b>>1;
i++;
}
return i;
}
int cmp(const void* a, const void* b) //快排的比较函数
{
return *(int*)a - *(int*)b;
}
int main()
{
int m,n;
while (scanf("%d", &m) != EOF) //m是网址的个数
{
getchar();
int ip[4][1002] = { 0 }; //ip有四行,每一行的每一个元素都是一个网址相同位置的数字
for (int i = 0;i < m;i++)
{
for (int j = 0;j < 4;j++) //读入
{
int num = 0;
char temp;
while ((temp = getchar()) != '.' && temp != '\n')num = num * 10 + temp - '0';//将读入的字符转化位整数
ip[j][i] = num;
}
}
int arr[4] = { 255,255,255,255 }; //子网掩码初始化为最大
for (int i = 0;i < 4;i++)
{
qsort(ip[i], m, sizeof(int), cmp); //排序每一行,找到位运算的开始数字
if (ip[i][0] != ip[i][m - 1]) //开始位运算
{
n = bit(ip[i][0], ip[i][m - 1]); //求得移动位数
ip[i][m - 1]=ip[i][m - 1] >> n; //先右移后左移,将后面的n位清零
ip[i][m - 1]=ip[i][m - 1] << n;
arr[i] = arr[i] >> n; //子网掩码清零
arr[i]=arr[i] << n;
printf("%d", ip[i][m - 1]); //输出
if (i != 3)printf(".");
for (i++;i < 4;i++) //后面的数字清零输出
{
printf("0");
arr[i] = 0;
if (i != 3)printf(".");
}
}
else {
printf("%d", ip[i][0]);
if (i != 3)printf(".");
}
}
printf("\n");
printf("%d.%d.%d.%d\n", arr[0], arr[1], arr[2], arr[3]);
}
}
IP Networks UVA - 1590的更多相关文章
- uva 1590 - IP Networks(IP地址)
习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32 ...
- UVa 1590 IP网络(简单位运算)
Description Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...
- 位运算基础(Uva 1590,Uva 509题解)
逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...
- [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...
- UVA 1590 IP Networks JAVA
题意:输入m代表接下来的数据个数,计算接下来输入数据的网络掩码,和最小网络地址. 思路:①子网掩码:先将数据转为二进制,判断从哪一位开始有数据不一样,记下下标index,则子网掩码是index的前面是 ...
- Uva 1590 IP Networks
这道题目是一道关于IP地址的题目,要深入理解这道题需要有一定的网络基础. 这道题目我第一次做的时候虽然也AC了,但代码写的比较复杂,不够精炼.近期刚刚参加了网络方面的培训,在有一定知识的基础上,又重写 ...
- Endless looping of packets in TCP/IP networks (Routing Loops)
How endless looping of packets in a TCP/IP network might occur? Router is a device used to interconn ...
- ease of rerouting traffic in IP networks without readdressing every host
https://en.wikipedia.org/wiki/Network_address_translation In the face of the foreseeable global IP a ...
- POJ 2799 IP Networks
network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢 ...
随机推荐
- 自己的Scrapy框架学习之路
开始自己的Scrapy 框架学习之路. 一.Scrapy安装介绍 参考网上资料,先进行安装 使用pip来安装Scrapy 在开始菜单打开cmd命令行窗口执行如下命令即可 pip install Scr ...
- GetQueuedCompletionStatus客户端前端和server之间的通信
项目中遇到了这个东西,怎么都调试不到.记录下. 一.完成端口IOCP https://www.cnblogs.com/yuanchenhui/p/iocp_windows.html
- JUC-ThreadLocal
目录 ThreadLocal ThreadLocal测试 ThreadLocal类结构 前言 多线程访问同一个共享变量的时候也别容易出现并发问题,特别是在多线程需要对一个共享变量进行写入的时候.为了保 ...
- Mac忘记密码
1.启动电脑的时候,按住 Command+R,直到苹果的图标出现,松开,等待进入... 2.直接点击菜单栏上有个功能里面有 "终端" 功能,点击打开. 3.在终端页面里输入---& ...
- C#类中的成员
@ 目录 字段 属性 方法 构造函数 类和对象的简单解释 创建类和对象 类中成员的归属问题 字段 字段的声明与声明变量类似,可以添加访问修饰符,通常情况下字段设置为私有的,然后定义属性对字段的读写进行 ...
- docker搭建redis集群和Sentinel,实现故障转移
0.引言 公司开发需要用到redis,虽然有运维自动搭建,还是记录下如何搭建redis集群和Sentinel. 采用的是vagrant虚拟机+docker的方式进行搭建. 搭建思路: 首先是借鉴下其他 ...
- 关于,java-webservice接口,根据服务端,自动生成客户端调用时,响应时间慢
我这边遇到的问题,是在和对方进行webservice接口交互的时候,用工具,调用对方的webservice接口,对方响应很快.但是用java生成的客户端调用就会很慢才得到响应.大概有5分钟左右. 这里 ...
- 越来越受欢迎的Vue想学么,90后小姐姐今儿来教你
摘要:Vue的相关技术原理成为了前端岗位面试中的必考知识点,掌握 Vue 对于前端工程师来说更像是一门"必修课". 本文原作者为尹婷,擅长前端组件库研发和微信机器人. 我们发现, ...
- JAVA_标识符、数据类型、变量
标识符和关键字 所有的标识符否应该以字母a ~ z和 A ~Z ,美元符($).下划线(_)开始. 首字符之后可以是字母a ~ z和 A ~Z ,美元符($).下划线(_)的任意字符组合. 注 ...
- Go Module实战:基于私有化仓库的GO模块使用实践
新年开工近一月,2021 年第一期 Open Talk 定档 3 月 18 日晚 8 点,本期我们邀请到了又拍云资深后端开发工程师刘云鹏和我们一起聊聊 Go 最新特性 Go Module 实战. 刘云 ...