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的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...

  2. uva 1590 - IP Networks(IP地址)

    习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32 ...

  3. IP Networks UVA - 1590

     Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he de ...

  4. OpenJudge/Poj 2105 IP Address

    1.链接地址: http://poj.org/problem?id=2105 http://bailian.openjudge.cn/practice/2105 2.题目: IP Address Ti ...

  5. 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 ...

  6. 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 ...

  7. UVA 1590 IP Networks JAVA

    题意:输入m代表接下来的数据个数,计算接下来输入数据的网络掩码,和最小网络地址. 思路:①子网掩码:先将数据转为二进制,判断从哪一位开始有数据不一样,记下下标index,则子网掩码是index的前面是 ...

  8. poj 2105 IP Address

    IP Address Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18951   Accepted: 10939 Desc ...

  9. Uva 1590 IP Networks

    这道题目是一道关于IP地址的题目,要深入理解这道题需要有一定的网络基础. 这道题目我第一次做的时候虽然也AC了,但代码写的比较复杂,不够精炼.近期刚刚参加了网络方面的培训,在有一定知识的基础上,又重写 ...

随机推荐

  1. linux----ln

    1.格式 ln source_file_path target_file_path 2.执行ln 命令的用户要对source_file_path有写权限,才可以创建软连接. 3.souce_file这 ...

  2. python---连接MySQL第一页

    前言:python如果想要连接到MySQL要安装上相关的驱动才下:好在驱动程序在mysql的官网可以下载的到. 以下是我自己保存的一个conetos7版本 http://yunpan.cn/cLACS ...

  3. delphi 对TThread扩充TSimpleThread

    对线程的使用,是每个开发者都应该熟练掌握的,也是进阶的重要一环. 可以这样说,没有线程,连界面假死的问题都解决不了,就更别谈并行处理来提高效率了. 本例对线程进行改进,打造一个基础的线程,以后线程应用 ...

  4. 从一个实例,看new FunctionName()的内部机制

    下面的代码: function Dog(name) { this.name = name; Dog.prototype = { shout: function() { alert("I am ...

  5. C语言入门(17)——C语言数组应用的一个实例

    本篇通过一个实例介绍使用数组的一些基本模式.问题是这样的:首先生成一列0-9的随机数保存在数组中,然后统计其中每个数字出现的次数并打印,检查这些数字的随机性如何.随机数在某些场合(例如游戏程序)中是非 ...

  6. java web基础环境搭建

    java web基础环境包括:(1)servlet容器也即tomcat(2)jre即java程序运行环境 环境变量配置:分别下载jdk和tomcat安装包. jdk环境变量配置: 第一步:系统环境变量 ...

  7. printdir-deldir-bmp

    #include<unistd.h> #include<stdio.h> #include<dirent.h> #include<string.h> # ...

  8. user Collaborative Filtering

    ---恢复内容开始--- 算法步骤: 1.计算用户相似度 2.对于特定用户,选出k个最相似的用户,将这些用户评价过的前k好的物品推荐给该用户   用户相似度 度量: 其中|N(u)|表示用户u评价过的 ...

  9. poj 3250 Bad Hair Day(单调队列)

    题目链接:http://poj.org/problem?id=3250 思路分析:题目要求求每头牛看见的牛的数量之和,即求每头牛被看见的次数和:现在要求如何求出每头牛被看见的次数? 考虑到对于某头特定 ...

  10. 优步北京B组(8月10日-8月16日奖励规则)

    奖励政策: [优步北京B组] 定义为2015年7月20日前激活的部分司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机( ...