1. 程式人生 > >UVA-IP Networks

UVA-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 ≤ 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地址,並且求出其子網掩碼;只需要求出這些地址的最大的IP地址和最小的IP地址,並且相與;

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>

using namespace std;

char MS[20], ms[20];

int main()
{
    int len;
    while(scanf("%d", &len) == 1){
        char s[20];
        int M[4], m[4];
        int num[4];
        int _len = 0;
        scanf("%s", s);
        strcpy(MS, s);
        strcpy(ms, s);
        //找出最大和最小的地址
        while(--len){
            scanf("%s", s);
            if(strcmp(MS, s) < 0)
                strcpy(MS, s);
            if(strcmp(ms, s) > 0)
                strcpy(ms, s);
        }
//        printf("%s %s\n", MS, ms);
        int lenth1 = strlen(MS);
        int lenth2 = strlen(ms);
        int cnt = 0;
        for(int i=0;i<lenth1;i++){
            int t = 0;
            while(MS[i]!='.' && i<lenth1){
                t = t*10 + (MS[i]-48);
                i++;
            }
            M[cnt++] = t;
        }
        cnt = 0;
        for(int i=0;i<lenth2;i++){
            int t = 0;
            while(ms[i]!='.' && i<lenth2){
                t = t*10 + (ms[i]-48);
                i++;
            }
            m[cnt++] = t;
        }
        for(int i=0;i<4;i++){
            num[i] = (M[i] ^ m[i]) ^ 255;
        }
        for(int i=0;i<4;i++){
            int temp[8];
            int cot = 0;
            while(num[i]){
                temp[cot++] = num[i]%2;
                num[i] /= 2;
            }
            int j;
            for(j=cot-1;j>=0;j--){
                if(!temp[j])
                    break;
                _len++;
            }
            if(j >= 0)
                break;
        }
        int k = 0;
        for(int i=0;i<4;i++){
            int sum = 0;
            for(int j=7;j>=0;j--){
                if(k >= _len)
                    break;
                int t = 1;
                t = t << j;
                sum += t;
                k++;
            }
            num[i] = sum;
        }//for
        printf("%d.%d.%d.%d\n", M[0]&num[0], M[1]&num[1]
               , M[2]&num[2], M[3]&num[3]);
        printf("%d.%d.%d.%d\n", num[0], num[1], num[2], num[3]);
    }//while

    return 0;
}