Description

 

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 0n32 . 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(1m1000) . 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

思路:这题就是位运算的运用,注意一下相同的情况。我是先确定四个数字中哪个不一样了,然后再去比较这个数字的二进制,确定第几位二进制不同。

 #include<iostream>
using namespace std; const int co = << ; int ans[][]; int main()
{
int n,t,k;
//freopen("D:\\txt.txt", "r", stdin);
while (scanf("%d", &n)!=EOF){
for (int i = ; i < n; i++)
{
scanf("%d.%d.%d.%d", &ans[i][], &ans[i][], &ans[i][], &ans[i][]);
} for (int i = ; i < ; i++)
{
t = ans[][i];
int ok = ;
k = i;
for (int j = ; j < n; j++)
{
if (ans[j][i] != t)
{
ok = ;
break;
}
}
if (!ok) break;
} int s = ;
int p = ;
for (int i = ; i >= ; i--)
{
t = (ans[][k] / s) & ;
for (int j = ; j < n; j++)
{
if (((ans[j][k] / s) & ) != t)
{
p = i;
}
}
s = s * ;
} int number = ;
int count = ;
s = ;
for (int i = ; i < p; i++)
{
if ((ans[][k] * s) & co)
{
number += << ( - i);
}
count += << ( - i);
s = s * ;
} if (k == )
{
printf("%d.%d.%d.%d\n", ans[][], ans[][], ans[][], number);
printf("255.255.255.%d\n", count);
}
else if (k == )
{
printf("%d.%d.%d.0\n", ans[][], ans[][], number);
printf("255.255.%d.0\n", count);
}
else if (k == )
{
printf("%d.%d.0.0\n", ans[][], number);
printf("255.%d.0.0\n", count);
}
else
{
printf("%d.0.0.0\n", number);
printf("%d.0.0.0\n", count);
}
}
}

最新文章

  1. MySQL二进制日志
  2. oracle 11g 通过在线重定义方式修改表结构
  3. samba 服务
  4. markdown语法学习效果预览
  5. 项目常用jquery/easyui函数小结
  6. python——类
  7. Windows 环境下于虚拟环境安装源码安装 cx_oracle
  8. ecshop改造读写分离
  9. 【HDU2815】【拓展BSGS】Mod Tree
  10. 微信电脑版(Mac和Windows)安装
  11. setcookie 设置无效
  12. CST 公共生成树
  13. year:2017 month:7 day:19
  14. 探寻 webpack 插件机制
  15. git修改本地和远程仓库名称的解决方法
  16. SE Springer小组之《Spring音乐播放器》可行性研究报告一、二(转载)
  17. Java基础编程题——素数
  18. Linux中使用Vim快速更换文档中Windows换行符为Linux平台
  19. ActiveReports 报表控件V12新特性 -- 可定制的安装设置
  20. Python核心编程——Chapter16

热门文章

  1. 2.搭建cassandra时遇到没有公网网卡的问题
  2. 十天精通CSS3(1)
  3. [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive
  4. windows7下docker配置镜像加速
  5. 剑指offer2
  6. liferay6.1.2的API
  7. 全局修改composer源地址
  8. eclipse导入spring框架
  9. 如何获得当前页的SITECORE上下文
  10. ASP.NET控件--DropDownList