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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值