UVA-1590 IP Networks

Source: Internet
Author: User
Tags convert ip to binary

IP Networks
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

Alex is administrator of IP networks. His clients has a bunch of individual IP addresses and he decided to group all those IPs addresses into the smallest possi BLE IP network.

Each IP address was a 4-byte number that's written byte-by-byte in a decimal dot-separated notation ' byte0.byte1.byte2.by Te3 "(quotes is added for clarity). Each byte are written as a decimal number from 0 to 255 (inclusive) without extra leading zeroes.

IP network is described by 4-byte numbers-network address and network mask. Both Network address and network mask is written in the same notation as IP addresses.

In order to understand the meaning of the network address and network mask, you had to consider their binary representation. Binary representation of IP address, network address, and network mask consists of bits:8 bits for byte0 (most signifi cant 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 0N . Network Mask always have + - n  First bits set to one, and N Last bits set to zero in its binary representation. Network address has arbitrary + - n  First Bits, and N Last bits set to zero in its binary representation. IP network contains all IP addresses whose + - n  First bits is equal to + - 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 would contain several test cases, each of the them as described below.

The first line of the input file contains a single integer number m(1m) . The following m lines contain IP addresses, one address on a line. Each IP address is appear more than once in the input file.

Output

For each test case, write to the output file of 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

Will enter all the IP into binary save, detect all IP, get it from the nth bit beginning to appear different, so, the minimum IP is, binary pre-32-n bit and other IP consistent, the last n bits are all 0.

The subnet mask is binary before the 32-n bit is 1, and the last n bits are all 0. Careful is good, no difficulty, do the word scanf really useful.

#include <iostream> #include <cstring> #include <string> #include <cstdio>using namespace std; int Ip[1010][4][35];int Ip[1010][5];int Main () {int m;while (scanf ("%d", &m)! = EOF) {for (int i = 0; i < m; i++) {SCA    NF ("%d.%d.%d.%d", &ip[i][0], &ip[i][1], &ip[i][2], &ip[i][3]); scanf Convenient completion input}for (int i = 0; i < m; i++) for (int j = 0; J < 4; J + +) for (int k = 0; k < 8; k++) {ip[i][j][7-k]                     = (Ip[i][j] & (1 << k)? 1:0); Convert IP to binary with array save}/*for (int i = 0; i < m; i++) {for (int j = 0; J < 4; J + +) {for (int k = 0; k < 8; k++) cout &LT;&L T Ip[i][j][k];cout << "";} cout << Endl;}  */int OK = 1;int I, j, k;for (i = 0; i < 4; i++)//Detected n{for (j = 0; J < 8; J + +) {for (k = 1; K < M; k++) if (ip[k-1][i][j]! = Ip[k][i][j]) {ok = 0; break;} if (!ok) break;} if (!ok) break;} cout << i << "<<j << endl;int ans_wz[4][8];int ans_ym[4][8];memset (ans_WZ, 0, sizeof (ANS_WZ));  Deal with the answer for (int II = 0; II < i; ii++) for (int JJ = 0; JJ < 8; jj++) ANS_WZ[II][JJ] = ip[0][ii][jj];for (int jj = 0; JJ < J; jj++) ANS_WZ[I][JJ] = Ip[0][i][jj];memset (ans_ym, 0, sizeof (ANS_YM)), for (int II = 0; II < i; ii++) for (int JJ = 0; JJ & Lt 8; jj++) ANS_YM[II][JJ] = 1;for (int jj = 0; JJ < J; jj++) ANS_YM[I][JJ] = 1;/*for (int i = 0; i < 4; i++) {for (int j = 0 ; J < 8; J + +) cout << ans_wz[i][j];cout << "";}  cout << endl;for (int i = 0; i < 4; i++) {for (int j = 0; J < 8; j + +) cout << ans_ym[i][j];cout << " ";}   cout << Endl;                 */int Ans_wz[4] = {0}; The answer is converted to binary int ans_ym[4] = {0};for (int i = 0; i < 4; i++) {for (int j = 0; J < 8; J + +) {Ans_wz[i] = ans_wz[i] * 2 + Ans_wz[i][j];ans_ym[i] = ans_ym[i] * 2 + ans_ym[i][j];}} for (int i = 0; i < 4; i++) {if (i) cout << '. '; cout << ans_wz[i];} cout << endl;for (int i = 0; i < 4; i++) {if (i) cout << '. '; cout << ans_ym[i];} cout << Endl;}}



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.