记录编号 7907 评测结果 AAAAAAAAAA
题目名称 IP网络管理员 最终得分 100
用户昵称 GravatarBYVoid 是否通过 通过
代码语言 C++ 运行时间 0.145 s
提交时间 2008-11-12 14:38:58 内存使用 0.26 MiB
显示代码纯文本
#include <iostream>

using namespace std;

const int MAX=1001;

int N,k,p,mk;
unsigned int P[MAX];

int commonprefix(unsigned int a,unsigned int b)
{
	int i,pa,pb;
	for (i=1;i<=32;i++)
	{
		pa=(a >> (32-i)) & 1;
		pb=(b >> (32-i)) & 1;
		if (pa!=pb)
			return i-1;
	}
	return 8;
}

int main()
{
	int i,j,a,b,c,d;
	unsigned int IP,msk,A;
	freopen("networkip.in","r",stdin);
	freopen("networkip.out","w",stdout);
	scanf("%d",&N);
	for (i=1;i<=N;i++)
	{
		scanf("%d.%d.%d.%d",&a,&b,&c,&d);
		P[i]=(a << 24)+(b << 16)+(c << 8)+d;
	}
	mk=100;
	for (i=1;i<=N;i++)
	{
		for (j=i+1;j<=N;j++)
		{
			k=commonprefix(P[i],P[j]);
			if (k<mk)
				mk=k;
		}
	}
	p=32-mk;
	IP=P[1];
	msk=0;
	for (i=1;i<=mk;i++)
		msk = (msk << 1) | 1;
	for (i=1;i<=p;i++)
		msk = msk << 1;
	IP &= msk;
	A=0xFF;
	printf("%d.%d.%d.%d\n%d.%d.%d.%d",(IP >> 24) & A,(IP >> 16) & A,(IP >> 8 ) & A,IP & A,
		(msk >> 24) & A,(msk >> 16) & A,(msk >> 8 ) & A,msk & A);
	return 0;
}