1590 - IP Networks

本文详细阐述了如何将一系列IP地址分组到最小的IP网络中,包括IP地址的表示方式、网络地址和网络掩码的概念、IP网络的定义及其大小比较方法,以及实现算法的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 - n first 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值