poj 2799 IP Networks 模拟

题意:

给一些ip,求包括他们的最小网络(网络号+子网掩码)。

分析:

关键是找到从高位到低位从哪位开始不同。

代码:

//poj 2799
//sep9
#include <iostream>
using namespace std;
typedef long long ll;
ll vis[40];

void print(ll x)
{
	ll a,b,c,d;
	d=x%(1<<8);
	x=x/(1<<8);	

	c=x%(1<<8);
	x=x/(1<<8);	

	b=x%(1<<8);
	x=x/(1<<8);	

	a=x;
	printf("%I64d.%I64d.%I64d.%I64d\n",a,b,c,d);

}

int main()
{
	int m;
	scanf("%d",&m);
	memset(vis,-1,sizeof(vis));
	while(m--){
		ll a,b,c,d;
		scanf("%I64d.%I64d.%I64d.%I64d",&a,&b,&c,&d);
		ll x=a*(1LL<<24)+b*(1LL<<16)+c*(1LL<<8)+d;
		for(int i=0;i<32;++i){
			int p=(x&(1LL));
			if(vis[i]==-1)
				vis[i]=p;
			else{
				if(vis[i]!=p)
					vis[i]=2;
			}
			x>>=1;
		}
	}
	ll i;
	for(i=31;i>=0;--i)
		if(vis[i]==2)
			break;
	ll ip=0,masks=0,d=1;
	for(ll j=0;j<32;++j){
		if(j>i){
			ip=ip+vis[j]*d;
			masks=masks+d;
		}
		d*=2;
	}
	print(ip);
	print(masks);
	return 0;
} 
时间: 2024-08-12 13:05:59

poj 2799 IP Networks 模拟的相关文章

poj 2799 IP Networks 模拟 位运算

poj链接:http://poj.org/problem?id=2799 这题实在是非常的有趣... 写的时候也非常的开心... 然后就写跪了... 刚好讲了ip地址和子网掩码的只是 整个学期的通信导论我就只有这节课有事没去结果昨晚把这方面的只是补起来了有种功德圆满的感觉 network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 其次是练习了各种移位操作 我发现移位操作是个非常好用的东西 因为它自填充0 所以对一个二进制数往右移8位

poj 2799 IP Networks (模拟/水题)

题目:给你一堆ip,求他们的最小网络地址和网络掩码 思路:找到最大的ip和最小的ip,把他们转化成二进制,从头到尾找二进制位相同的个数,最小网络地址把后面的不同的所有二进制变成0,网络掩码把前面的相同变成一 代码: #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; struct node { char k[35]

POJ 2799 IP Networks

IP Networks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2159   Accepted: 815 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

poj 3399 Product(模拟)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int cmp(int x,int y) { return x>y; } int main() { int a[110],a1[110],a2[110],ans[110]; int n,k,k1,k2,i,k3; while(~scanf("%d%d",&n,&k

poj 2632 Crashing Robots, 模拟

点击打开链接 简单 模拟机器人的移动,发生碰撞时输出相应的信息. code #include <cstdio> #include <cstring> using namespace std; struct node{ int k; int s; } mtx[200][200]; struct node1{ int x, y; } rob[200]; int n, m; int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; //N, E, S, W

【POJ】 Instant Complexity (模拟)

[POJ] Instant Complexity (模拟) Instant Complexity Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1905   Accepted: 657 Description Analyzing the run-time complexity of algorithms is an important tool for designing efficient programs that

POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)

题目链接:http://poj.org/problem?id=2632 先话说昨天顺利1Y之后,直到今天下午才再出题 TAT,真是刷题计划深似海,从此AC是路人- - 本来2632是道稍微恶心点的模拟,但毕竟是模拟,一般模拟都是只要示例过了基本就AC了,但这个题很特殊 我开始的时候一直跑不出测试示例,才发现题目中的插图和我构想的坐标系是不一样的(看来还不能轻易忽视掉插图啊) 这个题给出的坐标系是纵轴为y,横轴为x,y从下到上依次递增,x轴是从左到右递增 而我用的二维数组记录的地图,也就是说我的纵

uva 1590 - IP Networks(IP地址)

习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32个二进制位,前32-n位为1,后n位为0,网络地址的前32-n位任意,后n位为0. 所有前32-n位和网络地址相同的IP都属于此网络. 例如,网络地址为194.85.160.176(二进制为11000010|01010101|10100000|10110000), 子网掩码为255.255.255.

POJ 3030. Nasty Hacks 模拟水题

Nasty Hacks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13136   Accepted: 9077 Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of malicious software which teenagers may use to fool their friends.