UVA 1590
来源:互联网 发布:mac怎么下载歌 编辑:程序博客网 时间:2024/06/07 15:40
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 0n32 . 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(1m1000) . 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
#include <stdio.h>#include <string.h>#include <cstdio>#include <iostream>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;int zwym_table[9]={255, 254, 252, 248, 240, 224, 192, 128, 0};int zwym[5];int minip[5];int main (){ int m; while(scanf("%d",&m)!=EOF) { int i,s[10][1000],s2[10]={0}; memset(s,0,sizeof(s)); memset(zwym,0,sizeof(zwym)); memset(minip,0,sizeof(minip)); for(i=0;i<m;i++) { scanf("%d.%d.%d.%d",&s[0][i],&s[1][i],&s[2][i],&s[3][i]); } for(i=0;i<=3;i++) { int change=0; sort (s[i],s[i]+m); int p=s[i][0]; int q=s[i][m-1]; for(int j=1;j<9;j++) { if(p%2!=q%2) { change=j; } p/=2; q/=2; } zwym[i]=zwym_table[change]; minip[i]=s[i][0] & zwym[i]; } for(int i=0; i<4; i++) { if(zwym[i] != 255) { for(i = i+1; i<4; i++) { zwym[i] = 0; minip[i] = 0; } break; } } printf("%d.%d.%d.%d\n",minip[0], minip[1], minip[2], minip[3]); printf("%d.%d.%d.%d\n",zwym[0], zwym[1], zwym[2], zwym[3]); } return 0;}
- uva 1590
- UVA 1590
- UVA 1590
- UVA 1590 - IP Networks
- UVA 1590 IP Networks
- UVa 1590 - IP Networks
- UVA - 1590 IP Networks
- UVA 1590 - IP Networks
- UVa 1590 IP Networks
- Uva 1590 IP Networks
- UVA 1590 IP Networks
- Uva 1590 IP Networks
- UVa 1590 IP Networks
- UVA 1590 IP地址
- uva
- UVA
- UVA
- UVA
- android加固系列—1.如何检验so文件是否加壳成功
- 在安卓项目中集成高德地图时,获取SHA1值
- windows下全志 A20 Android script.fex 调试工具
- 收集的35个 jQuery 小技巧/代码片段
- Linux常用命令整理
- UVA 1590
- 链路状态路由协议,调用分发列表的时候,作用分析,distribute-list
- PAT 1003 Emergency
- 【集成学习】scikit-learn随机森林调参小结
- 重拾CSS(1)CSS中的margin属性
- c++ 凌驾于字符串之上的string类
- android加固系列—2.加固前先要学会破解,调试内存值修改程序走向
- oracle分析函数技术详解(配上开窗函数over())
- 百练2801解题报告---填词