UVA 1590-ip Networks (IP address)

Source: Internet
Author: User

Exercise 4-5 IP network (IP Networks, ACM/ICPC NEERC 2005, UVa1590)
You can describe a subnet (that is, a contiguous range of IP addresses) with a network address and a subnet mask. One of the subnets
The mask contains 32 bits, the front 32-n bit is 1, the last n bit is 0, the first 32-n bit of the network address is arbitrary, and the last n bit is 0.
All IP with the same previous 32-n bit and network address belongs to this network.
For example, the network address is 194.85.160.176 (binary is 11000010|01010101|10100000|10110000),
Subnet mask is 255.255.255.248 (binary is 11111111|11111111|11111111|11111000), the subnet
The IP address range is 194.85.160.176~194.85.160.183. Enter some IP addresses to find the smallest network (which includes IP
Least-Address network), which contains all of these input addresses.
For example, if you enter 3 IP addresses: 194.85.160.177, 194.85.160.183, and 194.85.160.178, including the 3
The network address of the smallest network addresses is 194.85.160.176, the subnet mask is 255.255.255.248.
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
"Note: He may have many group inputs, and there is no blank line between each set of outputs"

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category= 833&problem=4465&mosmsg=submission+received+with+id+21184770

Ideas:
1, the first to save all the IP, with arrays or containers or something.
2. Convert binary
3, starting from the first, you compare all the IP on this one on the same number no
4. Determine the minimum number of network bits, that is, the number of bits with a mask of 1.
5. Convert Decimal
(I use string to store the binary IP)
(using the next vector, of course, can also be stored in an array, an array of 32*1000)

/*110000100101010110100000_10110001110000100101010110100000_10110111110000100101010110100000_ 101100101100001001010101101000001011000011111111111111111111111111111000*///Special case: Enter only one IP address, then the mask should be 32 bits 1#include <iostream> #include <stack> #include <string> #include <vector> #include <cmath> #include    <cstdio>using namespace std;string binary (int dec) {string str= "00000000";    Stack<int> s;    int bin=0;        for (int i=7;i>=0;i--) {//s.push (dec%2);        str[i]=dec%2+ ' 0 ';    Dec/= 2;    }//while (!s.empty ())//{//bin = bin*10 + s.top ();//S.pop ();//}//return bin; return str;}    int decimal (string bin) {int dec = 0;    for (int i=0;i<8;i++)//From high start {dec + = (int) POW (2,8-i-1) * (bin[i]-' 0 '); } return Dec;    int main () {int T;        while (cin>>t) {vector<string>ip;//store all IP int num=0 for input;        if (t==1)//Prevent only entering one address for special cases num=32;     while (t--)//input   {int a[4];            String str;            scanf ("%d.%d.%d.%d", &a[0],&a[1],&a[2],&a[3]);            for (int i=0;i<4;i++) {str + = binary (a[i]);        } ip.push_back (str);            } for (int j=0;j<32;j++)//To determine the number of equal digits {int iff=0;                for (int i=1;i<ip.size (); i++) {//cout<<ip.at (i) <<endl;                if (ip.at (0) [j] = = ip.at (i) [j])//This is equal to {iff=1;                    } else {iff=0;                Break            }} if (IFF)//This bit is equal, the number of mask bits plus a num++;        else break; } string zero= "00000000";        Standby 0 string Mask,minip; int score[4];//stores the minimum network of four decimal IP address segments int score2[4];//Stores The mask of four decimal IP address segments for (int i=1;i<=32-num;i++)//start with the last one            0 {minip= ' 0 ' +minip;        mask= ' 0 ' +mask;            } for (int i=num-1;i>=0;i--)//inverted complement {minip=ip[0].at (i) +minip;        mask= ' 1 ' +mask; }//cout<<num<<endl;//for (int i=0;i<ip.size (); i++)//Cout<<ip[i]<<end        l;//cout<<endl;//cout<<mask<<endl;            for (int i=0;i<4;i++)//Operation min Network {score[i] =decimal (MINIP.SUBSTR (i*8,8));            Score2[i] =decimal (MASK.SUBSTR (i*8,8));            The following elimination methods are: Edge transformation, edge ratio is the number of bits to the smallest network bit. Compare the binary address of a minimal network above first, and then transform directly.                To be more complex//if (I*8+8>num)//{////cout<<i*8<< "" "<<num<<endl;// Cout<<ip[0].substr (i*8,num-i*8) << "|" <<zero.substr (0,32-num) <<endl;//score[i]=decimal (Ip[0].substr (i*8,num-i*8) +zero.substr (0,32-nu m));//}//else//{////cout<<ip[0].substr (i*8,8) <<endl;//score[i]=decimal (Ip[0].substr (i*8,8));/}} printf ("        %d.%d.%d.%d\n ", score[0],score[1],score[2],score[3]);    printf ("%d.%d.%d.%d\n", score2[0],score2[1],score2[2],score2[3]); } return 0;} AC at 2018/4/22

(digression: Fortunately last semester's network class is to calculate the IP address, so this problem I think of more than half an hour to have ideas (crying))

UVA 1590-ip Networks (IP address)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.