1590 - IP Networks

来源:互联网 发布:怎么跟网络管理员联系 编辑:程序博客网 时间:2024/05/17 09:04

Alex is administrator of IP networks. Hisclients have a bunch of individual IP addresses and he decided to group allthose IP addresses into the smallest possible IP network.

Each IP address is a 4-byte number that iswritten byte-by-byte in a decimal dot-separated notation``byte0.byte1.byte2.byte3" (quotes are added for clarity). Each byte iswritten as a decimal number from 0 to 255 (inclusive) without extra leadingzeroes.

IP network is described by two 4-byte numbers- network address and network mask. Both network address and network mask arewritten in the same notation as IP addresses.

In order to understand the meaning of networkaddress and network mask you have to consider their binary representation.Binary representation of IP address, network address, and network mask consistsof 32 bits: 8 bits for byte0 (most significant to least significant), followedby 8 bits for byte1, followed by 8 bits for byte2, and followed by 8 bits forbyte3.

IP network contains a range of 2n IPaddresses where 0n32 . Network maskalways has 32 - nfirst bits set to one, and n lastbits set to zero in its binary representation. Network address hasarbitrary 32 - n first bits, and n lastbits set to zero in its binary representation. IP network contains all IP addresseswhose 32 - n first bits are equal to 32 -n firstbits of network address with arbitrary n last bits. We saythat one IP network is smaller than the other IP network if it contains fewerIP addresses.

For example, IP network with network address194.85.160.176 and network mask 255.255.255.248 contains 8 IP addresses from194.85.160.176 to 194.85.160.183 (inclusive).

Input 

The input file will contain several testcases, each of them as described below.

The first line of the input file contains asingle integer number m (1m1000) . Thefollowing 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 filetwo lines that describe the smallest possible IP network that contains all IPaddresses from the input file. Write network address on the first line andnetwork 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 namespacestd;

 

unsigned int a[4],ip[1010];//使用无符号整型

unsigned int addr,mask;

int m;//全局变量慎用!!!

 

void operate();

voidprint(unsigned int num);

 

int main()

{

    char ch;//读取数字之间的点

    while(cin>>m)

    {

        mask=0xffffffff;//一定每次测试都要重新赋值!!!

        for(int i=0;i<m;i++)

        {

           cin>>a[0]>>ch>>a[1]>>ch>>a[2]>>ch>>a[3];

            ip[i]=(a[0]<<24)+(a[1]<<16)+(a[2]<<8)+(a[3]);

//位运算方便一定别忘记每个位运算都要加上括号

        }

        operate();

        print(addr);

        print(mask);

    }

}

 

void operate()

{

    bool flag=true;

    while(flag)

    {

        flag=false;

        addr=ip[0]&mask;

        for(int i=1;i<m;i++)

        {

            if((ip[i]&mask)!=addr)

            {

                flag=true;

                break;

            }

        }

        if(flag)

        {

            mask=mask<<1;

        }

    }

}

 

voidprint(unsigned int num)

{

    for(int i=0;i<3;i++)

    {

        cout<<((num>>((3-i)*8))&0xff)<<".";

    }

    cout<<(num&0xff)<<endl;

}

解析:

题意是:计算二进制的最大公共前缀。这里刚好能用无符号32位整型来存储IP,然后就是一些位运算的技巧。子网掩码mask初始化为0xffffffff。然后一直左移遍历,直到所有IP和当前mask对应的IP地址都相等时,退出循环。任何数&1得到其本身,&0得到0.

0 0