zoukankan      html  css  js  c++  java
  • BFS:UVa1590-IP Networks (子网掩码相关知识)

    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 has32 −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


    解题心得

    1. 这个题读懂题意是关键,里面涉及了一些子网掩码,IP地址相关的知识。借用别人关于这个题题意描述。
    2. 子网掩码是子网划分的依据,它跟IP地址一样,长度也是32位,点分十进制表示,每部分0~255,但是跟IP地址不同的是,子网掩码只能由连续的1和0组成,也就是说,把这32位从任意位置分开,左边只能全是1,右边只能全是0。比如11111111.11111111.11111111.11111000(255.255.255.248)就是合法的子网掩码,而11000000.10101000.00000001.00000000(192.168.1.0)就不合法(可以尝试一下在计算机上给网络连接手动分配子网掩码,看看它会怎么提示你)。给定两个IP,假设其子网掩码二进制有x个连续的1,则如果这两个IP的二进制前x位对应相等,那么这两个IP就属于同一网段,也就是属于同一个子网。
      如果给定一个子网掩码和一个IP,就可以求出这个IP所在子网的最小IP,方法是将IP的二进制与子网掩码的二进制进行按位与运算,原理是,子网掩码为1的二进制位,要求子网内所有IP的这一位必须全部相等,而子网掩码为0的位不作要求,也就是说,给定一个IP,子网内最小IP对应的子网掩码为1的位必须跟给定IP一样,按位与的时候,给定IP与子网掩码是1的位按位与后的结果不变,子网掩码0的位按位与后为0(恰好是最小),这样按位与运算结束后,得到的IP就是子网内最小IP(说的我自己都有点晕乎了,想弄明白这个,必须了解位运算和子网掩码的相关知识)。
      说了这么多,都只是预备知识,下面可以切入正题,给定一些IP地址,求出子网掩码和子网内最小IP。
      根据上面所说,这个题只要求出子网掩码,然后与给定的任意IP进行按位与运算,就可以得到最小IP了。那么现在关键就是求子网掩码了,既然给定的这一些IP都是一个网段的,那么找到这些IP里的最小IP和最大IP,然后找到这两个IP的二进制从左往右看哪一位最先出现不同(异或运算可解),就可以知道子网掩码里有几个连续的1,自然就得到子网掩码了,然后最小IP便迎刃而解。所以这个题其实很简单,只要了解点IP地址相关知识即可。

    然后自己写了一个贼他妈弱智的代码,感觉自己没有学过算法,居然过了。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 40;
    int num[1010][maxn];
    int ans[maxn];
    
    void change_bin_num(int num2,int t,int i)
    {
        while(num2)
        {
            if(num2%2)
                num[i][t] = 1;
            num2 /= 2;
            t--;
        }
    }
    
    void change_bin(int a,int b,int c,int d,int i)//将输入的全改成二进制
    {
        change_bin_num(d,32,i);
        change_bin_num(c,24,i);
        change_bin_num(b,16,i);
        change_bin_num(a,8,i);
    }
    
    void print_ans(int part)
    {
        int res = 1;
        int ans2 = 0;
        for(int i=part; i>part-8; i--)
        {
            ans2 += ans[i]*res;
            res *= 2;
        }
        printf("%d",ans2);
    }
    
    int get_ans1(int m)
    {
        int num2 = 0;
        bool flag = false;
        for(int i=1; i<=32; i++)
        {
            for(int j=1; j<=m; j++)
            {
                int now = num[1][i];
                if(now != num[j][i])
                {
                    flag = true;
                    break;
                }
            }
            if(flag)
                break;
            num2++;
        }
        return num2;
    }
    
    void print_ans2(int now,int num2)
    {
        int ans2 = 0;
        int res = 1;
        for(int i=now+num2; i>now; i--)
        {
            ans2 += num[1][i]*res;
            res *= 2;
        }
        printf("%d",ans2);
        if(now + num2 <= 24)
            printf(".");
    }
    
    void get_ans2(int num2)//打印最小的那个ip地址
    {
        int k = num2;
        int part = 0,now = 0;
        while(num2)
        {
            part++;
            if((num2-8) >= 0)
                print_ans2(now,8);
            else
            {
                for(int i=k+1; i<=32; i++)//子网掩码0的部分对应的全为0
                    num[1][i] = 0;
                print_ans2(now,8);
                break;
            }
            num2 -= 8;
            now += 8;
        }
    
        if(part >= 4)//记录打印了几部分,少的部分用0来填充
            return ;
        else
            for(int i=part; i<4; i++)
            {
                printf("0");
                if(i != 3)
                    printf(".");
            }
    }
    
    int main()
    {
        int m;
        while(~scanf("%d",&m))
        {
            memset(num,0,sizeof(num));
            memset(ans,0,sizeof(ans));
            for(int i=1; i<=m; i++)
            {
                int a,b,c,d;
                scanf("%d.%d.%d.%d",&a,&b,&c,&d);//输入一点小技巧
                change_bin(a,b,c,d,i);
            }
            int _num = get_ans1(m);//得到子网掩码最左边1的个数
            get_ans2(_num);
            printf("
    ");
    
            //打印子网掩码
            for(int i=1; i<=_num; i++)
                ans[i] = 1;
            print_ans(8);
            printf(".");
            print_ans(16);
            ````
    rintf(".");
            print_ans(24);
            printf(".");
            print_ans(32);
            printf("
    ");
        }
        return 0;
    }
    

    大神的简洁代码

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    int zwym_table[9] = {255, 254, 252, 248, 240, 224, 192, 128, 0};//二进制中前8位全是1的值,前7位,前6位......
    
    int main()
    {
        int ip[4][1024];
        int m;
        while(scanf("%d",&m)!=EOF)
        {
            memset(ip, 0, sizeof(ip));
            int zwym[4];
            int minip[4];
            for(int i=0; i<m; 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++)
            {
                int dif=0, x, j;
                int p,q;
                sort(ip[i], ip[i]+m);
                p = ip[i][m-1];
                q = ip[i][0];
                for(j=1; j<=8; j++)
                {
                    if(p%2!=q%2)//得出不相等的位置
                        dif = j;
                    p = p/2;
                    q = q/2;
                }
                zwym[i] = zwym_table[dif];//对应相应的值
                minip[i] = ip[i][0] & zwym[i];
            }
            for(int i=0; i<4; i++)
            {
                if(zwym[i] != 255)
                {
                    for(i = i+1; i<4; i++)
                    {
                        zwym[i] = 0;
                        minip[i] = 0;
                    }
                    break;
                }
            }
            printf("%d.%d.%d.%d
    ",minip[0], minip[1], minip[2], minip[3]);
            printf("%d.%d.%d.%d
    ",zwym[0], zwym[1], zwym[2], zwym[3]);
        }
        return 0;
    }
    
  • 相关阅读:
    css display:flex 属性
    手机端设计规范(750*1334)
    太阳系动画
    关于调用三方平台接口与推送接口的总结<二>(2020.7.27)
    关于调用三方平台接口与推送接口的总结(2020.7.25)
    IntelliJ IDEA 常用快捷键列表及技巧大全
    JAVA中的泛型
    神兽保佑!
    zookeeper的图形化工具ZooInspector的使用
    Initialization failed for 'https://start.spring.io' Please check URL, network and proxy settings. Error message: Cannot download 'https://start.spring.io': connect timed out问题解决方案
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107275.html
Copyright © 2011-2022 走看看