UVa 1590 IP网络(简单位运算)
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网络(简单位运算)的更多相关文章
- uva 10718 Bit Mask (位运算)
uva 10718 Bit Mask (位运算) Problem A Bit Mask Time Limit 1 Second In bit-wise expression, mask is a ...
- uva 1590 - IP Networks(IP地址)
习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32 ...
- Uva 1590 IP Networks
这道题目是一道关于IP地址的题目,要深入理解这道题需要有一定的网络基础. 这道题目我第一次做的时候虽然也AC了,但代码写的比较复杂,不够精炼.近期刚刚参加了网络方面的培训,在有一定知识的基础上,又重写 ...
- UVA 10718 Bit Mask 贪心+位运算
题意:给出一个数N,下限L上限U,在[L,U]里面找一个整数,使得N|M最大,且让M最小. 很明显用贪心,用位运算搞了半天,样例过了后还是WA,没考虑清楚... 然后网上翻到了一个人家位运算一句话解决 ...
- UVA - 13022 Sheldon Numbers(位运算)
UVA - 13022 Sheldon Numbers 二进制形式满足ABA,ABAB数的个数(A为一定长度的1,B为一定长度的0). 其实就是寻找在二进制中满足所有的1串具有相同的长度,所有的0串也 ...
- UVA 1590 IP Networks JAVA
题意:输入m代表接下来的数据个数,计算接下来输入数据的网络掩码,和最小网络地址. 思路:①子网掩码:先将数据转为二进制,判断从哪一位开始有数据不一样,记下下标index,则子网掩码是index的前面是 ...
- 小米oj 反向位整数(简单位运算)
反向位整数 序号:#30难度:一般时间限制:1000ms内存限制:10M 描述 输入32位无符号整数,输出它的反向位. 例,输入4626149(以二进制表示为00000000010001101001 ...
- 位运算基础(Uva 1590,Uva 509题解)
逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...
- 三十天学不会TCP,UDP/IP网络编程-UDP,从简单的开始
如果对和程序员有关的计算机网络知识,和对计算机网络方面的编程有兴趣,欢迎去gitbook(https://www.gitbook.com/@rogerzhu/)star我的这一系列文章,虽然说现在这种 ...
随机推荐
- Java-小技巧-002-String 转 long
1.转化 long l = Long.parseLong([String]); 相当于 long l = Long.parseLong([String],10); long l = Long.valu ...
- qsv转换为mp4
1:下载 装换工具:http://www.downza.cn/soft/27484.html 2:双击打开exe可执行程序. 3:添加要转换的文件,和转换后要存储的位置 4:开始转换,转换为flv格 ...
- 机器学习理论基础学习5--- PCA
一.预备知识 减少过拟合的方法有:(1)增加数据 (2)正则化(3)降维 维度灾难:从几何角度看会导致数据的稀疏性 举例1:正方形中有一个内切圆,当维度D趋近于无穷大时,圆内的数据几乎为0,所有的数据 ...
- web前端面试小结(1)
两天大概面试了4家,有电面也有F2F,现将面试中的问题大概汇总下,一方面了解自己的不足,一方面用来勉励自己后面面试加油! 答案网上都有,就不一一写在这里了,后面有时间会把下面的问题分别拉出来详述. 1 ...
- 3:2 OGNL 简介
OGNL : (对象图导航语言) 从一个对象到另一个对象 OGNL来源于Xwork: OGNL的作用: OGNL在数据进出值栈的时候进行类型转换
- 2:5 视图控制器result的配置
result: 1:其实底层还是使用原来servlet的转发和重定向的方法: 2:redirectAction:只能定位到 action (比如下面name属性为 *User 的Action ,但 ...
- meta 如何写
阻止手机号加下划线,可拨打:<meta name="format-detection" content="telephone=no" /> (io ...
- python3 字典的常用方法
字典的方法(可能需要重新整理) 函数 说明 D代表字典对象 D.clear() 清空字典 D.pop(key) 移除键,同时返回此键所对应的值 D.copy() 返回字典D的副本,只复制一层(浅拷 ...
- MySQL root账户密码设为“root”后执行命令提示ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改root账户密码为“root”后,提示ERROR 1820 (HY000): You must reset your password using ALTER USER statement bef ...
- python中的对象(三)
一.python对象 python使用对象模型来存储数据.构造任何类型的值都是一个对象. 所有python对象都拥有三个特性:身份.类型.值 身份:每个对象都有一个唯一的身份标识自己,任何对象的身份可 ...