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 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
思路:这题就是位运算的运用,注意一下相同的情况。我是先确定四个数字中哪个不一样了,然后再去比较这个数字的二进制,确定第几位二进制不同。
#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我的这一系列文章,虽然说现在这种 ...
随机推荐
- 007-ant design 对象属性赋值,双向绑定
1.state对象属性赋值 设对象为 state={ datavalue:{ id:'', name:'', }, } 修改对象属性为: SetName=(e)=>{ let val=e.tar ...
- [vue]v-bind: sytle/class-bind&属性值绑定
v-bind - style绑定 - class绑定 - 属性值绑定 <!DOCTYPE html> <html lang="en"> <head&g ...
- 最大流(EK)
最大流 — Edmond Karp算法 Edmond Karp算法的大概思想: 反复寻找源点s到汇点t之间的增广路径,若有,找出增广路径上每一段[容量-流量]的最小值delta,若无,则结束. 在寻找 ...
- WebService之Axis2 (3):使用services.xml文件发布WebService
用Axis2实现Web Service,虽然可以将POJO类放在axis2\WEB-INF\pojo目录中直接发布成Web Service,这样做不需要进行任何配置,但这些POJO类不能在任何包中.这 ...
- 借root之名,行流氓之实,劝告,root需谨慎
20160425++++++ 今日再回头看这篇文章,貌似有点偏激了一点,不过xda论坛上有个疑似kingroot开发团队的用户说明了kingroot确实对supersu做了限制,说是supersu在替 ...
- java文件生成
package com.gcy.test.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileNo ...
- 生成word附件和word域动态赋值
生成word文档和word域动态赋值,很多时候需要生成这样的word文档供下载和打印,先制作一个包含了域的 word的模板附件,放在服务器端或者字节流存入数据库,以供需要的时候代码可以获取到,如: 其 ...
- 【JavaScript】数组随机排序 之 Fisher–Yates 洗牌算法
Fisher–Yates随机置乱算法也被称做高纳德置乱算法,通俗说就是生成一个有限集合的随机排列.Fisher-Yates随机置乱算法是无偏的,所以每个排列都是等可能的,当前使用的Fisher-Yat ...
- 解析分布式锁之Zookeeper实现(一)
实现分布式锁目前有三种流行方案,分别为基于数据库.Redis.Zookeeper的方案,本文主要阐述基于Zookeeper的分布式锁,其他两种会在后文中一起探讨.现在我们来看下使用Zookeeper如 ...
- 0-5v转0-20ma和0-5v转4-20ma
0-5v转0-20ma 0-5v转4-20ma