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);
}
}
}

UVa 1590 IP网络(简单位运算)的更多相关文章

  1. uva 10718 Bit Mask (位运算)

    uva 10718  Bit Mask  (位运算) Problem A Bit Mask Time Limit 1 Second In bit-wise expression, mask is a ...

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

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

  3. Uva 1590 IP Networks

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

  4. UVA 10718 Bit Mask 贪心+位运算

    题意:给出一个数N,下限L上限U,在[L,U]里面找一个整数,使得N|M最大,且让M最小. 很明显用贪心,用位运算搞了半天,样例过了后还是WA,没考虑清楚... 然后网上翻到了一个人家位运算一句话解决 ...

  5. UVA - 13022 Sheldon Numbers(位运算)

    UVA - 13022 Sheldon Numbers 二进制形式满足ABA,ABAB数的个数(A为一定长度的1,B为一定长度的0). 其实就是寻找在二进制中满足所有的1串具有相同的长度,所有的0串也 ...

  6. UVA 1590 IP Networks JAVA

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

  7. 小米oj 反向位整数(简单位运算)

     反向位整数 序号:#30难度:一般时间限制:1000ms内存限制:10M 描述 输入32位无符号整数,输出它的反向位. 例,输入4626149(以二进制表示为00000000010001101001 ...

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

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

  9. 三十天学不会TCP,UDP/IP网络编程-UDP,从简单的开始

    如果对和程序员有关的计算机网络知识,和对计算机网络方面的编程有兴趣,欢迎去gitbook(https://www.gitbook.com/@rogerzhu/)star我的这一系列文章,虽然说现在这种 ...

随机推荐

  1. [py][mx]django添加后台课程机构页数据-图片上传设置

    分析下课程页前台部分 机构类别-目前机构库中没有这个字段,需要追加下 所在地区 xadmin可以手动添加 课程机构 涉及到机构封面图, 即图片上传media设置, 也需要在xadmin里手动添加几条 ...

  2. __import__ 与动态加载 python module

    原文出处: koala bear    Direct use of __import__() is rare, except in cases where you want to import a m ...

  3. 微信小程序性能测试之jmeter踩坑秘籍(前言)

    最近要做个微信小程序的性能压测,虽然之前只做过web端的,但想一想都是压后端的接口,所以果断答应了下来,之前对jmeter都是小打小闹,所以趁着这次机会好好摆弄摆弄. ---------------- ...

  4. python图片处理(一)

    python图片处理需要先在cmd里面安装Pillow pip install Pillow 一.图片的打开与显示 from PIL import Image img=Image.open('d:/d ...

  5. 当输入域失去焦点 (blur) 时改变其颜色

    $("input").blur(function(){ $("input").css("background-color","#D ...

  6. 9/252D图的画法

    我们在介绍之前先想想2D图的一些元素 我在这里按我的思路写下一些: 坐标轴(尺度,区间..),线条(样式,颜色...),图和线的标签和注释,图像大小,图像里图片的排版(一张图像中多张图) 下面我们将分 ...

  7. linux 启动引导流程

    课程大纲: Linux引导流程 Linux运行级别 Linux启动服务管理 GRUB配置与应用 启动故障分析与解决 系统引导流程 1.固件firmware(CMOS(固化在硬件上的程序与硬件统称)/B ...

  8. Linux系统——JumpServer跳板机的搭建和部署

    公网源部署jumpserver跳板机 建立阿里云公网源yum仓库(服务端)[root@localhost ~]# lsanaconda-ks.cfg install.log.syslog jumpse ...

  9. Git—使用方法

    1.:插件的安装(eclipse LUNA版本之后已经自动集成,不需要安装插件). * 先打开该网页提供了对应版本的EGit,自己选择相应的版本.(http://wiki.eclipse.org/EG ...

  10. codeforces E - Anya and Cubes 分块处理 暴力搜索

    说的是给了n个立方体,立方体从1标号到n,每个立方体上有一个数字, 你有 k 个机会 使得其中 k个数位他们自己的阶乘,(自然使用可以少于k次机会,每个立方体最多被使用1次) ,那么求出你从这n个立方 ...