【UVA 1590 --- IP Networks】位运算

【UVA 1590 --- IP Networks】位运算

题目来源:点击进入【UVA 1590 — IP Networks】

Description

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 ≤ 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 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 ≤ m ≤ 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中相同的32-n位。相应的子网掩码前32-n位都是1。后面n位子网掩码自然为0。
然后ip我们就只需要将每一位与子网掩码相与(&)即可求出ip。

AC代码:

#include <stdio.h>
#include <string.h>
const int MAXN = 1e3+5;
int mask[4],arr_ip[MAXN][4],ip[4];
int arr_mask[9]={255,254,252,248,240,224,192,128,0};

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(mask,0,sizeof(mask));
        memset(ip,0,sizeof(ip));
        for(int i=0;i<n;i++)
            scanf("%d.%d.%d.%d",&arr_ip[i][0],&arr_ip[i][1],&arr_ip[i][2],&arr_ip[i][3]);
        for(int i=0;i<4;i++)
        {
            int mi=0x3f3f3f3f,mx=0;
            for(int j=0;j<n;j++)
            {
                if(mi>arr_ip[j][i]) mi=arr_ip[j][i];
                if(mx<arr_ip[j][i]) mx=arr_ip[j][i];
            }
            int x=mx^mi,ix;
            for(int j=0;j<=8;j++)
                if(!(x>>j)) { ix=j; break;}
            mask[i]=arr_mask[ix];
            if(mask[i]!=255) break;
        }
        for(int i=0;i<4;i++)
            ip[i]=arr_ip[0][i]&mask[i];
        printf("%d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]);
        printf("%d.%d.%d.%d\n",mask[0],mask[1],mask[2],mask[3]);
    }
    return 0;
}
发布了412 篇原创文章 · 获赞 135 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104268620