UVA 1590 IP Networks

Source: Internet
Author: User

Type: Water problem

Test instructions: Given a set of IP addresses, in a subnet, find its subnet number and net mask.

Analysis: Each byte is judged; the same sequence number of multiple IPs is stored in vectors, sorted, and judged by the first and last identical to determine whether the two masks are the mask of this byte.

Code:

1 intGet_mask (intFstintLastint&IP)2 {3     intMask =0xFF;4      for(intCT =8; CT >0; CT--)5     {6         if((FST & mask) = = (Last &mask))7              Break;8Mask <<=1;9     }Tenip = FST &Mask; One     return(Mask &0xFF); A } -  - intMain () { the  -     intT; -vector<int> ar_vec[4]; -      for(; scanf ("%d", &t)! =EOF;) +     { - GetChar (); +          for(inti =0; I <4; i++) A ar_vec[i].clear (); at          for(inti =0; i < T; i + +) -         { -             intN1, N2, N3, N4; -scanf"%d.%d.%d.%d", &n1, &n2, &n3, &N4); - GetChar (); -ar_vec[0].push_back (N1); inar_vec[1].push_back (n2); -ar_vec[2].push_back (n3); toar_vec[3].push_back (N4); +         } -         intmask, IP; the         intPos; *          for(pos =0; POS <4; POS + +) $         {Panax Notoginseng sort (Ar_vec[pos].begin (), Ar_vec[pos].end ()); -             intSize =ar_vec[pos].size (); the             intFST = ar_vec[pos][0]; +             intLast = Ar_vec[pos][size-1]; A             if(FST! =Last ) the             { +Mask =Get_mask (FST, last, IP); -                  Break; $             } $         } -         intar_ip[4]; -         intar_mask[4]; the          for(intK =0; K < Pos; K + +) -Ar_ip[k] = ar_vec[k][0];Wuyi         if(Pos <4) theAr_ip[pos] =IP; -          for(intK = pos +1; K <4; K + +) WuAR_IP[K] =0; -         //Mask About          for(intK =0; K < Pos; K + +) $AR_MASK[K] =255; -         if(Pos <4) -Ar_mask[pos] =Mask; -          for(intK = pos +1; K <4; K + +) AAR_MASK[K] =0; +printf"%d.%d.%d.%d\n", ar_ip[0], ar_ip[1], ar_ip[2], ar_ip[3]); theprintf"%d.%d.%d.%d\n", ar_mask[0], ar_mask[1], ar_mask[2], ar_mask[3]); -     } $     return 0; the}

Coding Errors and WA:

1. Get to the qualifying location should immediately break, otherwise it will be + +.

2. Think of the time will remember XXX, will forget!!!: To do a mask, 0xFF, the left shift should be 0XFE, assigned to int, left to 0x1fe, to mask char is correct, but the return of Forgetting & 0xFF, did not remove the high-level 0x1.

Lesson: Non-equivalence changes are prone to errors and avoided as much as possible.

3. When assigning a value to an array, consider whether the subscript is out of bounds: when pos = = 4 o'clock, it immediately crosses the border.

UVA 1590 IP Networks

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.