IP Networks
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2350 | Accepted: 905 |
Description
Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he decidedto 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 from0 to 255 (inclusive) without extra leading zeroes.
IP network is described by two 4-byte numbers - network address and network mask. Both networkaddress 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 <= n <= 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 lastbits. 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).
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 from0 to 255 (inclusive) without extra leading zeroes.
IP network is described by two 4-byte numbers - network address and network mask. Both networkaddress 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 <= n <= 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 lastbits. 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 first line of the input contains a single integer number m (1 <= m <= 1000). The following m lines contain IP addresses, one address on a line. Each IP address may appear more than once in a case.
Output
Write to the output two lines that describe the smallest possible IP network that contains all IP addresses from this case. Write network address on the first line and network mask on the secondline.
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
Source
题意:给定n表示将要输入的ip个数,求此ip集合的 子网掩码 和该网段中数值最小的ip地址。
思路:ip由32位2进制组成,子网掩码也由32位2进制组成,且子网掩码的1全部在左边,0全部在右边。
设所有输入的ip二进制情况下前n位相同,则子网掩码前n位为1,后32-n位为0.由此可求出子网掩码。
而网段最小ip的求法更简单,前n位与其他ip相同 后32-n位为0,用任意ip与子网掩码按位与即可。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct ip_node{
int d_ip[4];
int b_ip[32];
};
ip_node ipset[1005];
int mt[9] = {0, 128, 192, 224, 240, 248, 252, 254, 255};
void getb_ip(int n)
{
for(int k=0;k<n;k++)
{
for(int i=1;i<=4;i++)
{
int tmp=ipset[k].d_ip[i-1];
for(int j=i*8-1;tmp>0&&j>=(i-1)*8;j--)
{
ipset[k].b_ip[j]=tmp%2;
tmp>>=1;
}
}
}
}
int check(int n)
{
int i,j;
for(i=0;i<32;i++)
{
for(j=1;j<n;j++)
{
if(ipset[j].b_ip[i]!=ipset[0].b_ip[i])
{
return i;
}
}
}
return i;
}
ip_node getmask(int p)
{
ip_node mask;
for(int i=1;i<=4;i++)
{
int sum=0;
for(int j=i*8-1;j>=(i-1)*8;j--)
{
if(j<p)
sum++;
}
mask.d_ip[i-1]=mt[sum];
}
return mask;
}
int main()
{
int n,i,j,p;
int tmp[4];
ip_node min_ip,mask;
while(~scanf("%d",&n))
{
memset(ipset,0,sizeof(ipset));
for(i=0;i<n;i++)
{
scanf("%d.%d.%d.%d",&tmp[0],&tmp[1],&tmp[2],&tmp[3]);
for(j=0;j<4;j++)
{
ipset[i].d_ip[j]=tmp[j];
}
}
getb_ip(n);
p=check(n);
mask=getmask(p);
printf("%d.",ipset[0].d_ip[0]&mask.d_ip[0]);
printf("%d.",ipset[0].d_ip[1]&mask.d_ip[1]);
printf("%d.",ipset[0].d_ip[2]&mask.d_ip[2]);
printf("%d\n",ipset[0].d_ip[3]&mask.d_ip[3]);
printf("%d.",mask.d_ip[0]);
printf("%d.",mask.d_ip[1]);
printf("%d.",mask.d_ip[2]);
printf("%d\n",mask.d_ip[3]);
}
return 0;
}