POJ 2799 IP Networks
network address是前(32-n)随意 后n位全零
network mask是前(32-n)全一 后n位全零
本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢出,取反后可以作为mask的枚举然后拿mask和mins或者maxs并一下就得到address了。
代码:
#include <cstdio>
#include <iostream>
using namespace std; const int maxn = 70; int main()
{ int n;
while(scanf("%d", &n) == 1)
{
unsigned int maxs = 0;
unsigned int mins = 0-1;
//printf("%u\n",mins);
int a, b, c, d;
unsigned int e;
for(int i = 0; i < n; i++)
{
scanf("%d.%d.%d.%d", &a, &b, &c, &d);
e = ((unsigned int)a << 24) + (b << 16) + (c << 8) + d;
//printf("%u\n",e);
if(e < mins)
mins = e;
if(e > maxs)
maxs = e;
}
unsigned int mask;
for (int i = 0; i <= 32; i++)
{
mask = ~((1ULL<<i)-1U);//注意这里ULL,是为了防止数据溢出
//printf("%u\n",mask);
if ((mins & mask) == (maxs & mask))
break;
}
unsigned int ans = mins & mask; printf("%u.%u.%u.%u\n", ans >> 24, (ans << 8) >> 24, (ans << 16) >> 24, (ans << 24) >> 24);
printf("%u.%u.%u.%u\n", mask >> 24, (mask << 8) >> 24, (mask << 16) >> 24, (mask << 24) >> 24); } return 0;
}
POJ 2799 IP Networks的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...
- uva 1590 - IP Networks(IP地址)
习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32 ...
- IP Networks UVA - 1590
Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he de ...
- OpenJudge/Poj 2105 IP Address
1.链接地址: http://poj.org/problem?id=2105 http://bailian.openjudge.cn/practice/2105 2.题目: IP Address Ti ...
- Endless looping of packets in TCP/IP networks (Routing Loops)
How endless looping of packets in a TCP/IP network might occur? Router is a device used to interconn ...
- ease of rerouting traffic in IP networks without readdressing every host
https://en.wikipedia.org/wiki/Network_address_translation In the face of the foreseeable global IP a ...
- UVA 1590 IP Networks JAVA
题意:输入m代表接下来的数据个数,计算接下来输入数据的网络掩码,和最小网络地址. 思路:①子网掩码:先将数据转为二进制,判断从哪一位开始有数据不一样,记下下标index,则子网掩码是index的前面是 ...
- poj 2105 IP Address
IP Address Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18951 Accepted: 10939 Desc ...
- Uva 1590 IP Networks
这道题目是一道关于IP地址的题目,要深入理解这道题需要有一定的网络基础. 这道题目我第一次做的时候虽然也AC了,但代码写的比较复杂,不够精炼.近期刚刚参加了网络方面的培训,在有一定知识的基础上,又重写 ...
随机推荐
- centos 添加epel、remi仓库和ELRepo仓库
centos使用yum安装软件非常方便,yum会自动安装软件的相关依赖.但是centos自带的源仓库,软件相对老旧并且不太全,所以我们可以添加第三方仓库,可以安装较新的软件版本. epel是fedor ...
- PHP简易计算器方法1
<?phpheader("content-type:text/html;charset=utf-8");session_start();?><!DOCTYPE h ...
- mongo设计(三)
原文:http://blog.mongodb.org/post/88473035333/6-rules-of-thumb-for-mongodb-schema-design-part-3 By Wil ...
- skynet的流程1
logpath = "."harbor = 1address = "127.0.0.1:2526"master = "127.0.0.1:2013&q ...
- linux 驱动模块 内核编译环境
目录(?)[+] Linux设备驱动Hello World程序介绍 如何编写一个简单的linux内核模块和设备驱动程序.我将学习到如何在内核模式下以三种不同的方式来打印hello world,这三种方 ...
- undo_retention:确定最优的撤销保留时间
使用下面的公式来计算undo_retention参数的值: undo_retention=undo size/(db_block_size * undo_block_per_sec) 可以通过提交下面 ...
- logstorm
http://blog.itpub.net/15480802/viewspace-688859/ http://www.csdn.net/article/2014-09-04/2821558
- poj 3250 Bad Hair Day(单调队列)
题目链接:http://poj.org/problem?id=3250 思路分析:题目要求求每头牛看见的牛的数量之和,即求每头牛被看见的次数和:现在要求如何求出每头牛被看见的次数? 考虑到对于某头特定 ...
- 高级new创建
myclass *p = new(pcathe)myclass[10];//限定区域分配内存,覆盖模式,可以避免内存泄漏 #include <iostream> class myclass ...
- 网易云课堂_程序设计入门-C语言_第五周:函数_1分解质因数
1 分解质因数(5分) 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. ...