其他分享
首页 > 其他分享> > UVA 1590 IP Networks 位运算

UVA 1590 IP Networks 位运算

作者:互联网

//位运算 
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<stdlib.h> 
using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main()
{
	int m;
	while(~scanf("%d", &m))
	{
		uint a, b, c, d, i;
		uint t, minn = 0-1, maxx = 0;
		
		for(i = 0; i < m; i++)
		{
			scanf("%u.%u.%u.%u", &a, &b, &c, &d); 
			t =  (a<<24) + (b<<16) + (c<<8) + d; //位运算的优先级不高,括号不能少 
			//地址的二进制前32-n位一定是和maxx和minn的前32-n位一致的,通过maxx和mann求出n 
			minn = minn > t ? t : minn;
			maxx = maxx < t ? t : maxx;
		}
		//遍历求n 
		uint mask;
		for(i = 0; i <= 32; i++)
		{
			mask = ~(((1ULL)<<i)-1U);  //ULL不能少  
			if( (mask&minn) == (mask&maxx))
			{
				break;
			}
		} 
		uint add = (long long)(minn&maxx) >> i << i;  //long long 不能少 
		printf("%u.%u.%u.%u\n", add>>24, add<<8>>24, add<<16>>24, add<<24>>24);
		printf("%u.%u.%u.%u\n", mask>>24, mask<<8>>24, mask<<16>>24, mask<<24>>24); 			
	}
	return 0;
}

标签:24,maxx,mask,.%,uint,1590,UVA,include,Networks
来源: https://www.cnblogs.com/hucorz/p/14346667.html