UVA 1590 - IP Networks
2015-08-03 12:05
441 查看
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 - nfirst 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).
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.
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 - nfirst 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
如果给定一个子网掩码和一个IP,就可以求出这个IP所在子网的最小IP,方法是将IP的二进制与子网掩码的二进制进行按位与运算,原理是,子网掩码为1的二进制位,要求子网内所有IP的这一位必须全部相等,而子网掩码为0的位不作要求,也就是说,给定一个IP,子网内最小IP对应的子网掩码为1的位必须跟给定IP一样,按位与的时候,给定IP与子网掩码是1的位按位与后的结果不变,子网掩码0的位按位与后为0(恰好是最小),这样按位与运算结束后,得到的IP就是子网内最小IP
求子网掩码了,既然给定的这一些IP都是一个网段的,那么找到这些IP里的最小IP和最大IP,然后找到这两个IP的二进制从左往右看哪一位最先出现不同(异或运算可解),就可以知道子网掩码里有几个连续的1,自然就得到子网掩码了,然后最小IP便迎刃而解
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int n; int ip[4][1010]; int a[4],minip[4]; while(scanf("%d",&n)!=EOF) { memset(ip,0,sizeof(ip)); memset(a,0,sizeof(a)); memset(minip,0,sizeof(minip)); for(int i=0;i<n;i++) scanf("%d.%d.%d.%d",&ip[0][i],&ip[1][i],&ip[2][i],&ip[3][i]); for(int i=0;i<4;i++) { sort(ip[i],ip[i]+n); int q=ip[i][0]; int p=ip[i][n-1]; int dif=0; for(int j=1;j<=8;j++) { if(q%2 != p%2) dif=j; //最高第几位不同 q/=2; p/=2; } int sum=1,cj=1; for(int j=1;j<dif;j++) { sum*=2; cj+=sum; } if(dif==0) cj=0; a[i]=255-cj; minip[i]=a[i]&ip[i][0]; } for(int i=0;i<4;i++) //这个很重要 { if(a[i]!=255) { for(int j=i+1;j<4;j++) { a[j]=0; minip[j]=0; } break; } } printf("%d.%d.%d.%d\n",minip[0],minip[1],minip[2],minip[3]); printf("%d.%d.%d.%d\n",a[0],a[1],a[2],a[3]); } return 0; }
相关文章推荐
- Mono for Unreal Engine发布,C#进入虚幻引擎(Unreal Engine)
- Memory barrier(内存屏障)
- cocos2dx(3.X)中调用OpenGL
- Delphi函数重载时参数类型问题
- PAT 1030. Travel Plan (30)
- oracle不能够上下左右按键删除并且readline安装后还是找不到的问题
- EQueue - 一个纯C#写的分布式消息队列介绍2
- Unity,UDK,Unreal Engine4或者CryENGINE——我应该选择哪一个游戏引擎
- JavaScript快速入门(一)(2015.8.3)
- 1001 二分查找
- ios多线程开发的常用三种方式
- 同步和条件变量
- try-catch-finally的使用
- 客户端和服务器通讯 方法(2)
- Morris Traversal方法遍历二叉树
- NGUI初探之UIRoot
- Linux - EXT2文件系统
- Linux - EXT2文件系统
- Android 简单sd卡浏览器
- 图论浅析--最小生成树之Kruskal