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
题意:
概念一大堆,读懂之后总结起来几句话:
1.一个IP地址有一个子网掩码,几个相邻的IP地址共用一个子网掩码。这题让求最小IP和子网掩码。
2.一段IP地址最小那个和最大那个的二进制表示首位不同的地方是一个特殊位置。
3.2中的特殊位置它的左边(包括它)全是1,右边全是0,这是一段IP 的子网掩码的构成。得出子网掩码再和任意IP按位与即得最小IP。
4.这题有个坑,数据可以多次出现,当只有一个数据时,子网掩码是255.255.255.255,最小IP是这个数据,当只有一个数据多次出现时,和上一种情况一样。坑爹啊!!!因为这个错了7次......
AC代码:(位运算很精辟哦,好不容易想的)
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
struct ip{
int b[4];
}a[1050];
int cmp(ip a,ip b)
{
if(a.b[0]!=b.b[0])
return a.b[0]<b.b[0];
if(a.b[1]!=b.b[1])
return a.b[1]<b.b[1];
if(a.b[2]!=b.b[2])
return a.b[2]<b.b[2];
return a.b[3]<b.b[3];
}
int main()
{
freopen("ip.in","r",stdin);
freopen("ip.out","w",stdout);
int n;
while(scanf("%d",&n)!=-1)
{
for(int i=0;i<n;i++)
scanf("%d.%d.%d.%d",&a[i].b[0],&a[i].b[1],&a[i].b[2],&a[i].b[3]);
sort(a,a+n,cmp);
ip ansip;
int ans=0,flag=4;//flag初始成4,没有特殊位置前面全是0
for(int i=0;i<4;i++)
{ int t=a[0].b[i]^a[n-1].b[i];
if(t==0)continue;//异或相同则继续
else{ans=t;flag=i;break;}//异或不同找到第几个8位不同及异或结果
}
//cout<<ans<<"!1!"<<endl;
int ans1=0;
while(ans>0)//找到异或结果最左边那一位
{
ans=(ans>>1);//大于0就右移
ans1++;//移动一次就加一,得到的是从右边数最左1的位置
}
int zu=flag;
for(int i=0;i<zu;i++)//前整数组赋值全1
ansip.b[i]=255;
ansip.b[zu]=256-(int )pow(2,ans1);//有1有0那组用这个算,推一推就能推出来
for(int i=zu+1;i<4;i++)//右边全0
ansip.b[i]=0;
ip temp;
for(int i=0;i<4;i++)
temp.b[i]=255;
temp.b[0]=(ansip.b[0]&a[0].b[0]);
temp.b[1]=(ansip.b[1]&a[0].b[1]);
temp.b[2]=(ansip.b[2]&a[0].b[2]);
temp.b[3]=(ansip.b[3]&a[0].b[3]);
for(int j=0;j<4;j++)
{cout<<temp.b[j];if(j!=3)cout<<".";}
cout<<endl;
for(int i=0;i<4;i++)
{
cout<<ansip.b[i];
if(i!=3)cout<<".";
}cout<<endl;
}
return 0;
}