UVa 1590 IP网络(简单位运算)

Description

Download as PDF
 

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$ le$n$ le$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$ le$m$ le$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 #include<iostream>
 3 using namespace std;
 4 
 5 const int co = 1 << 7;
 6 
 7 int ans[1005][4];
 8 
 9 int main()
10 {
11     int n,t,k;
12     //freopen("D:\txt.txt", "r", stdin);
13     while (scanf("%d", &n)!=EOF){
14         for (int i = 0; i < n; i++)
15         {
16             scanf("%d.%d.%d.%d", &ans[i][0], &ans[i][1], &ans[i][2], &ans[i][3]);
17         }
18 
19 
20         for (int i = 0; i < 4; i++)
21         {
22             t = ans[0][i];
23             int ok = 1;
24             k = i;
25             for (int j = 1; j < n; j++)
26             {
27                 if (ans[j][i] != t)
28                 {
29                     ok = 0;
30                     break;
31                 }
32             }
33             if (!ok)  break;
34         }
35 
36 
37         int s = 1;
38         int p = 8;
39         for (int i = 7; i >= 0; i--)
40         {
41             t = (ans[0][k] / s) & 1;
42             for (int j = 1; j < n; j++)
43             {
44                 if (((ans[j][k] / s) & 1) != t)
45                 {
46                     p = i;
47                 }
48             }
49             s = s * 2;
50         }
51 
52 
53         int number = 0;
54         int count = 0;
55         s = 1;
56         for (int i = 0; i < p; i++)
57         {
58             if ((ans[0][k] * s) & co)
59             {
60                 number += 1 << (7 - i);
61             }
62             count += 1 << (7 - i);
63             s = s * 2;
64         }
65 
66 
67         if (k == 3)
68         {
69             printf("%d.%d.%d.%d
", ans[0][0], ans[0][1], ans[0][2], number);
70             printf("255.255.255.%d
", count);
71         }
72         else if (k == 2)
73         {
74             printf("%d.%d.%d.0
", ans[0][0], ans[0][1], number);
75             printf("255.255.%d.0
", count);
76         }
77         else if (k == 1)
78         {
79             printf("%d.%d.0.0
", ans[0][0], number);
80             printf("255.%d.0.0
", count);
81         }
82         else
83         {
84             printf("%d.0.0.0
", number);
85             printf("%d.0.0.0
", count);
86         }
87     }
88 }
原文地址:https://www.cnblogs.com/zyb993963526/p/6305868.html