博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 1590 IP Networks
阅读量:5305 次
发布时间:2019-06-14

本文共 2768 字,大约阅读时间需要 9 分钟。

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$ \le$n$ \le$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).

 

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$ \le$m$ \le$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.

 

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.

 

3 194.85.160.177 194.85.160.183 194.85.160.178

 

194.85.160.176 255.255.255.248
 
 
思路:
ip的四个部分分别处理,每一部分找出最大值的最小值,然后判断是其二进制最后几位不同,得出子网掩码;
用任意一个IP与子网掩码进行按位与运算得出最小IP;
 
 
#include 
#include
#include
#include
#include
using namespace std;int zwym_table[9] = {255, 254, 252, 248, 240, 224, 192, 128, 0};int main(){ int ip[4][1024]; int m; while(scanf("%d",&m)!=EOF) { memset(ip, 0, sizeof(ip)); int zwym[4]; int minip[4]; for(int i=0; i

转载于:https://www.cnblogs.com/kunsoft/p/5312802.html

你可能感兴趣的文章
开发进度一
查看>>
MyBaits学习
查看>>
管道,数据共享,进程池
查看>>
CSS
查看>>
[LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
查看>>
[Cypress] Stub a Post Request for Successful Form Submission with Cypress
查看>>
程序集的混淆及签名
查看>>
判断9X9数组是否是数独的java代码
查看>>
00-自测1. 打印沙漏
查看>>
UNITY在VS中调试
查看>>
SDUTOJ3754_黑白棋(纯模拟)
查看>>
Scala入门(1)Linux下Scala(2.12.1)安装
查看>>
如何改善下面的代码 领导说了很耗资源
查看>>
Quartus II 中常见Warning 原因及解决方法
查看>>
php中的isset和empty的用法区别
查看>>
Android ViewPager 动画效果
查看>>
pip和easy_install使用方式
查看>>
博弈论
查看>>
Redis sentinel & cluster 原理分析
查看>>
我的工作习惯小结
查看>>