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的更多相关文章

  1. uva 1590 - IP Networks(IP地址)

    习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32 ...

  2. UVa 1590 IP网络(简单位运算)

    Description   Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...

  3. 位运算基础(Uva 1590,Uva 509题解)

    逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...

  4. [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...

  5. UVA 1590 IP Networks JAVA

    题意:输入m代表接下来的数据个数,计算接下来输入数据的网络掩码,和最小网络地址. 思路:①子网掩码:先将数据转为二进制,判断从哪一位开始有数据不一样,记下下标index,则子网掩码是index的前面是 ...

  6. Uva 1590 IP Networks

    这道题目是一道关于IP地址的题目,要深入理解这道题需要有一定的网络基础. 这道题目我第一次做的时候虽然也AC了,但代码写的比较复杂,不够精炼.近期刚刚参加了网络方面的培训,在有一定知识的基础上,又重写 ...

  7. 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 ...

  8. 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 ...

  9. POJ 2799 IP Networks

    network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢 ...

随机推荐

  1. 配置mysql数据库时出再错误:LookupError: No installed app with label 'admin'.

    版本: windows10+py37+django2.2 错误: 项目启动时出现,No installed app with label 'admin' 解决办法: 安装最新的 pip install ...

  2. Android Studio|IntelliJ IDEA Git使用小技巧

    一 分支管理 1. 新建分支 在master的基础上创建新分支dev 2. 推送分支 将新建的分支dev推送到远程 3. 切换分支 4. 合并分支 当我们在dev分支完成代码修改并测试通过后 需要将d ...

  3. SpineRuntime-Presentation - 基于 spine-libgdx 实现在 AndroidPresentation 上展示 Spine 动画

    SpineRuntime-Presentation 基于 spine-libgdx 实现在 AndroidPresentation 上展示 Spine 动画 Github地址 效果 可以在 Andro ...

  4. FutureTask源码分析(JDK7)

    总览 A cancellable asynchronous computation. This class provides a base implementation of {@link Futur ...

  5. Mongo的相关语法

    mongod的条件操作符 $gt -------- greater than > $gte --------- gt equal >= $lt -------- less than < ...

  6. 如何在 Istio 中支持 Dubbo、Thrift、Redis 以及任何七层协议?

    赵化冰,腾讯云高级工程师,Istio Member,ServiceMesher管理委员,Istio 项目贡献者, Aerika 项目创建者 ,热衷于开源.网络和云计算.目前主要从事服务网格的开源和研发 ...

  7. Prometheus自定义指标

    1.  自定义指标 为了注册自定义指标,请将MeterRegistry注入到组件中,例如: public class Dictionary { private final List<String ...

  8. FreeBSD WIFI 配置详细介绍

    首先运行ifconfig,看看能不能找到你的网卡,如果能,那么你可以走了﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉运行sysctl net.wlan.devices,他可以告诉你,找到的无线网卡编辑/b ...

  9. 学习jQuery(1)

    学习jQuery 通过 jQuery,您可以选取(查询,query) HTML 元素,并对它们执行"操作"(actions). jQuery 语法 jQuery 语法是为 HTML ...

  10. x64dbg 条件断点相关文档

    输入 字符格式 条件断点 Input When using x64dbg you can often use various things as input. Commands Commands ha ...