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. Docker Elasticsearch 集群配置

    一:选用ES原因 公司项目有些mysql的表数据已经超过5百万了,各种业务的查询入库压力已经凸显出来,初步打算将一个月前的数据迁移到ES中,mysql的老数据就物理删除掉. 首先是ES使用起来比较方便 ...

  2. Redis操作指南

    目录 Redis安装与使用教程 一.Redis介绍 1.redis安装 2.redis与mysql的异同 3.redis与memcache的异同 二.Redis操作 1.启动服务 2.密码管理 3.连 ...

  3. Django模型层2

    目录 一.聚合查询 聚合函数 二.分组查询 利用group by进行分组查询 三.F与Q查询 1. F类 2. Q类 四.orm字段及参数 五.自定义char字段 六.orm中的事务操作 1. 什么是 ...

  4. Django练习遇到的错误记录

    _reverse_with_prefix() argument after ** must be a mapping, not set 错误代码: def get_absolute_url(self) ...

  5. wxWidgets源码分析(6) - 窗口关闭过程

    目录 窗口关闭过程 调用流程 关闭文档 删除视图 删除文档对象 关闭Frame App清理 多文档窗口的关闭 多文档父窗口关闭 多文档子窗口关闭 窗口的正式删除 窗口关闭过程总结 如何手工删除view ...

  6. 基于QT的全自动超声波焊接机上位机追溯系统(已经在设备上应用)

    应用说明: 本上位机程序是我在做锂电池产线项目的时候开发的,用于采集设备数据以及实现设备自动控制,下位机采用基恩士PLC,超声波机采用上海一家的超声波焊接机,实现电芯极耳的自动焊接,上位在设备焊接过程 ...

  7. KDE 桌面不显示背景和状态栏

    在arch的一次更新后,kde的桌面背景,插件,状态栏变成了一片黑,如下图所示. 解决办法:删除/home/stone/.config/plasma-org.kde.plasma.desktop-ap ...

  8. SpringMVC-01 什么是SpringMVC

    SpringMVC-01 什么是SpringMVC 回顾MVC 1.什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑 ...

  9. python中函数与方法的区别

    在python中,其实函数和方法的区别取决于其调用者,在普通的函数定义中就叫做函数 例如: def func(): print('这是一个函数') 而在一个类中定义时,就将其分为两种情况 第一种:被称 ...

  10. python数据分析三剑客基础之matpoltlib初解

    一.什么是matplotlib? python的底层绘图工具,主要做数据可视化图表,源自matplot. 二.为什么要学matplotlib? 1.它能将数据进行可视化,更直观的呈现出来 2.它能让数 ...