UVA 1590 - IP Networks



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$ \le$n$ \le$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$ \le$m$ \le$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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值