UVA - 1590 IP Networks

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 Status

Description

技术分享

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 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 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(1技术分享m技术分享1000) . 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

将输入所有IP转为二进制保存, 检测所有IP,得出其从第n位开始出现不同,于是,最小IP即,二进制前32-n位与其他IP一致,后n位全为0.

子网掩码是二进制前32-n位为1,后n位全为0。细心就好,没什么难度,做题的话scanf真的好用。

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
int ip[1010][4][35];
int IP[1010][5];
int main()
{
	int m;
	while (scanf("%d", &m) != EOF)
	{
		for (int i = 0; i < m; i++)
		{
			scanf("%d.%d.%d.%d", &IP[i][0], &IP[i][1], &IP[i][2], &IP[i][3]);    //scanf方便的完成输入
		}
		for (int i = 0; i < m; i++)
		for (int j = 0; j < 4; j++)
		for (int k = 0; k < 8; k++)
		{
			ip[i][j][7 - k] = (IP[i][j] & (1 << k) ? 1 : 0);                     //将IP转换为二进制用数组保存
		}
		
		/*
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				for (int k = 0; k < 8; k++)
					cout << ip[i][j][k];
				cout << " ";
			}	cout << endl;
		}
		*/

		int ok = 1;
		int i, j, k;
		for (i = 0; i < 4; i++)                               //检测出n
		{
			for (j = 0; j < 8; j++)
			{
				for (k = 1; k < m; k++)
				if (ip[k - 1][i][j] != ip[k][i][j])
				{
					ok = 0; break;
				}
				if (!ok) break;
			}
			if (!ok) break;
		}

		//cout << i << " " <<j << endl;

		int ans_wz[4][8];
		int ans_ym[4][8];

		memset(ans_wz, 0, sizeof(ans_wz));                    //依题处理出答案
		for (int ii = 0; ii < i; ii++)
		for (int jj = 0; jj < 8; jj++)
			ans_wz[ii][jj] = ip[0][ii][jj];
		for (int jj = 0; jj < j; jj++)
			ans_wz[i][jj] = ip[0][i][jj];

		memset(ans_ym, 0, sizeof(ans_ym));
		for (int ii = 0; ii < i; ii++)
		for (int jj = 0; jj < 8; jj++)
			ans_ym[ii][jj] = 1;
		for (int jj = 0; jj < j; jj++)
			ans_ym[i][jj] = 1;

		/*
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 8; j++)
				cout << ans_wz[i][j];
			cout << " ";
		}
		cout << endl;

		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 8; j++)
				cout << ans_ym[i][j];
			cout << " ";
		}
		cout << endl;
	   */

		int ans_WZ[4] = { 0 };                 //答案转换为二进制
		int ans_YM[4] = { 0 };
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 8; j++)
			{
				ans_WZ[i] = ans_WZ[i] * 2 + ans_wz[i][j];
				ans_YM[i] = ans_YM[i] * 2 + ans_ym[i][j];
			}
		}
	
		for (int i = 0; i < 4; i++)
		{
			if (i) cout << '.';
			cout << ans_WZ[i];
		}
		cout << endl;

		for (int i = 0; i < 4; i++)
		{
			if (i) cout << '.';
			cout << ans_YM[i];
		}
		cout << endl;

	}
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。