-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-1590.cpp
43 lines (41 loc) · 843 Bytes
/
UVA-1590.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<cstdio>
unsigned int arr[1010];
int iparr[4];
int main() {
int m,i,j,k;
unsigned int ip,mask,maski,net;
while(scanf("%d", &m) == 1) {
for(i=0; i<m; ++i) {
scanf("%d.%d.%d.%d", iparr, iparr+1, iparr+2, iparr+3);
arr[i] = 0;
for(j=0; j<4; ++j) {
arr[i] <<= 8;
arr[i] |= iparr[j];
}
if(i == 0) {
mask = -1;
maski = 0;
net = arr[0] & mask;
} else {
while(net != (arr[i] & mask)) {
maski++;
mask = ~((1 << maski) - 1);
if(maski == 32)
mask = 0;
net = net & mask;
}
}
}
for(i=3; i>=0; --i) {
iparr[i] = net % 256;
net >>= 8;
}
printf("%d.%d.%d.%d\n", iparr[0], iparr[1], iparr[2], iparr[3]);
for(i=3; i>=0; --i) {
iparr[i] = mask % 256;
mask >>= 8;
}
printf("%d.%d.%d.%d\n", iparr[0], iparr[1], iparr[2], iparr[3]);
}
return 0;
}