poj 2799 IP Networks

 

 

Description

Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he decidedto 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 from0 to 255 (inclusive) without extra leading zeroes. 
IP network is described by two 4-byte numbers - network address and network mask. Both networkaddress 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 lastbits. 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 first line of the input 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 a case.

Output

Write to the output two lines that describe the smallest possible IP network that contains all IP addresses from this case. Write network address on the first line and network mask on the secondline.
 
 

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地址一样,长度也是32位,点分十进制表示,每部分0~255,但是跟IP地址不同的是,子网掩码只能由连续的1和0组成,也就是说,把这32位从任意位置分开,左边只能全是1,右边只能全是0。比如11111111.11111111.11111111.11111000(255.255.255.248)就是合法的子网掩码,而11000000.10101000.00000001.00000000(192.168.1.0)就不合法(可以尝试一下在计算机上给网络连接手动分配子网掩码,看看它会怎么提示你)。给定两个IP,假设其子网掩码二进制有x个连续的1,则如果这两个IP的二进制前x位对应相等,那么这两个IP就属于同一网段,也就是属于同一个子网。

如果给定一个子网掩码和一个IP,就可以求出这个IP所在子网的最小IP,方法是将IP的二进制与子网掩码的二进制进行按位与运算,原理是,子网掩码为1的二进制位,要求子网内所有IP的这一位必须全部相等,而子网掩码为0的位不作要求,也就是说,给定一个IP,子网内最小IP对应的子网掩码为1的位必须跟给定IP一样,按位与的时候,给定IP与子网掩码是1的位按位与后的结果不变,子网掩码0的位按位与后为0(恰好是最小),这样按位与运算结束后,得到的IP就是子网内最小IP(说的我自己都有点晕乎了,想弄明白这个,必须了解位运算和子网掩码的相关知识)。

说了这么多,都只是预备知识,下面可以切入正题,给定一些IP地址,求出子网掩码和子网内最小IP。

根据上面所说,这个题只要求出子网掩码,然后与给定的任意IP进行按位与运算,就可以得到最小IP了。那么现在关键就是求子网掩码了,既然给定的这一些IP都是一个网段的,那么找到这些IP里的最小IP和最大IP,然后找到这两个IP的二进制从左往右看哪一位最先出现不同(异或运算可解),就可以知道子网掩码里有几个连续的1,自然就得到子网掩码了,然后最小IP便迎刃而解。所以这个题其实很简单,只要了解点IP地址相关知识即可。

 

 

代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 int zwym_table[9] = {255, 254, 252, 248, 240, 224, 192, 128, 0};
 9 
10 int main()
11 {
12     int ip[4][1024];
13     int m;
14 
15     while(scanf("%d",&m)!=EOF)
16     {
17 
18         memset(ip, 0, sizeof(ip));
19         int zwym[4];
20         int minip[4];
21 
22         for(int i=0; i<m; i++)
23             scanf("%d.%d.%d.%d",&ip[0][i], &ip[1][i], &ip[2][i], &ip[3][i]);
24 
25         for(int i=0; i<4; i++)
26         {
27 
28             int dif=0, x, j;
29             int p,q;
30 
31             sort(ip[i], ip[i]+m);
32 
33             p = ip[i][m-1];
34             q = ip[i][0];
35 
36             for(j=1; j<=8; j++)
37             {
38                 if(p%2!=q%2)dif = j;
39                 p = p/2;
40                 q = q/2;
41             }
42 
43             zwym[i] = zwym_table[dif];
44             minip[i] = ip[i][0] & zwym[i];
45         }
46 
47         for(int i=0; i<4; i++)
48         {
49             if(zwym[i] != 255)
50             {
51 
52                 for(i = i+1; i<4; i++)
53                 {
54                     zwym[i] = 0;
55                     minip[i] = 0;
56                 }
57 
58                 break;
59             }
60         }
61 
62         printf("%d.%d.%d.%d\n",minip[0], minip[1], minip[2], minip[3]);
63         printf("%d.%d.%d.%d\n",zwym[0], zwym[1], zwym[2], zwym[3]);
64 
65     }
66 
67     system("pause");
68     return 0;
69 }

 

posted @ 2016-05-04 18:31  Not-Bad  阅读(467)  评论(0编辑  收藏  举报