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. 简单梳理下 Vue3 的新特性

    在 Vue3 测试版刚刚发布的时候,我就学习了下 Composition API,但没想到正式版时隔一年多才出来,看了一下发现还是增加了不少新特性的,在这里我就将它们一一梳理一遍. 本文章只详细阐述 ...

  2. Element-UI远程搜索功能详解

    官方代码: <template> <div> <el-autocomplete v-model="state" :fetch-suggestions= ...

  3. Python爬虫系统化学习(3)

    一般来说当我们爬取网页的整个源代码后,是需要对网页进行解析的. 正常的解析方法有三种 ①:正则匹配解析 ②:BeatuifulSoup解析 ③:lxml解析 正则匹配解析: 在之前的学习中,我们学习过 ...

  4. Go的map

    目录 map 一.map的创建 1.map的类型 2.定义并初始化 二.给map添加元素 三.获取map的元素 四.删除map的元素 五.获取map的长度 六.map的类型 七.map的相等性 八.循 ...

  5. RabbitMQ基础教程

    目录 RabbitMQ相关概念介绍 生产者和消费者 队列 交换器.路由键.绑定 交换器类型 RabbitMQ运转流程 AMQP协议介绍 AMQP生产者流转过程 AMQP消费者流转过程 安装Rabbit ...

  6. VMware 安装 CentOS7 后的简单配置

    1.连网 如果能连网,跳过此步 试着ping一下百度 ping baidu.com 动态分配 IP sudo vim /etc/sysconfig/network-scripts/ifcfg-ens3 ...

  7. 从头捋了一遍 Java 代理机制,收获颇丰

    尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...

  8. 184. 部门工资最高的员工 + join + in

    184. 部门工资最高的员工 LeetCode_MySql_184 题目描述 题解分析 1.首先需要使用group by找出工资最高的值 2. 然后考虑到最高工资的可能有多位,所以使用in语句找到所有 ...

  9. Hadoop的常用命令

    注:hadoop的使用命令包含 hadoop fs 开头 or hdfs dfs开头 等多种形式来操作. 这里以hadoo fs的形式来介绍在这些命令 1.列出根目录下所有的目录或文件 hadoop ...

  10. 初识Java多线程

    一.多线程概述 1.1.程序.进程.线程概念 1)程序 是为完成特定任务,用某种语言编写的一组指令的集合,即指一段静态的代码,静态对象. 2)进程 是指一个内存中运行的应用程序,每个进程都有一个独立的 ...