poj 2799 IP Networks

IP Networks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2307 Accepted: 880

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

Source


题意:给你很多ip地址,然后让你用最小的一组  possible IP network表示一下
思路:根据题目中的提示可以简化为这两句话
找到ip的最大值和最小值,
network address:两者二进制中前面一样的部分保留,从第一个不一样的地方往后的所有位为0;
network mask :两者二进制中前面一样的部分全变为1,从第一个不一样的地方往后的所有位为0;

ac代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
int p1[50],p2[50],p3[50],p4[50];
int main()
{
    freopen("ip.in ","r",stdin);
    freopen("ip.out","w",stdout);
    int n,a,b,c,d;
    unsigned int min=0-1;
    unsigned int max=0;
    cin>>n;
    while(n--)
    {
        scanf("%d.%d.%d.%d",&a,&b,&c,&d);
        unsigned int g=(a<<24)+(b<<16)+(c<<8)+d;
        if(g>max)
        max=g;
        if(g<min)
        min=g;
    }
    int k=31;
    for(int i=1;i<=32;i++)
    {
      p1[k--]=max%2;
      max=max/2;
    }
    k=31;
    for(int i=1;i<=32;i++)
    {
        p2[k--]=min%2;
        min=min/2;
    }
    int flag=0;
    for(int i=0;i<32;i++)
    {
        if(p1[i]==p2[i]&&flag==0)
        p3[i]=p2[i];
        else
        {
            p3[i]=0;flag=1;
        }
    }
    flag=0;
    for(int i=0;i<32;i++)
    {
        if(p1[i]==p2[i]&&flag==0)
        p4[i]=1;
        else
        {
            p4[i]=0;flag=1;
        }
    }
    flag=7;
    int sum=0;
    for(int i=0;i<32;i++)
    {
       if(flag==-1)
       {
           printf("%d.",sum);
           flag=7;sum=0;
       }
       sum+=p3[i]*pow(2,(double)flag);
       flag--;
       if(i==31)
       {
           printf("%d\n",sum);
           break;
       }
    }
    flag=7;
    sum=0;
    for(int i=0;i<32;i++)
    {
       if(flag==-1)
       {
           printf("%d.",sum);
           flag=7;sum=0;
       }
       sum+=p4[i]*pow(2,(double)flag);
       flag--;
       if(i==31)
       {
           printf("%d\n",sum);
           break;
       }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值