这道题目是一道关于IP地址的题目,要深入理解这道题需要有一定的网络基础。

这道题目我第一次做的时候虽然也AC了,但代码写的比较复杂,不够精炼。近期刚刚参加了网络方面的培训,在有一定知识的基础上,又重写了这道题目。将很多步骤通过位运算(如移位,异或)进行了简化,在此贴一下前后两次的代码。

第二次代码:

 #include <cstdio>
const int maxn = + ;
int IPs[maxn][];
int find_firstdiff(int m){
for(int i=;i<;i++)
for(int j=;j<m;j++)
if(IPs[j][i]!=IPs[][i])return i;
return ;
}
void printIP(int ser,int equ,int k){
int is_first=;
for(int i=;i<;i++){
if(is_first)printf(".");else is_first=;
if(i==k)printf("%d",equ);
else if (i<k) printf("%d",ser==-?IPs[][i]:ser);
else printf("");
}
printf("\n");
}
int main(){
int m;
while(scanf("%d",&m)!=EOF){
for(int i = ; i < m; i++)
scanf("%d.%d.%d.%d",&IPs[i][],&IPs[i][],&IPs[i][],&IPs[i][]);
int k =find_firstdiff(m);
int maxi=,mini=;
for(int i = ; i < m ; i++){
if(IPs[maxi][k] < IPs[i][k])maxi= i;
if(IPs[mini][k] > IPs[i][k])mini= i;
}
int c=,t=IPs[maxi][k];
if(maxi!=mini){
int d = IPs[maxi][k] ^ IPs[mini][k];
c=;
if(!d)c=;
else while(d){
d/=;
c++;
}
t= IPs[maxi][k] >>c <<c;
}
printIP(-,t,k);
printIP(,-(<<c),k);
}
return ;
}

第一次代码:

 #include <cstdio>
#include <cstring>
#define maxn 1000+5
int s[maxn][];
void getBit(int *s,int num){
int n=;
while(num){
s[n--]=num%;
num/=;
} }
int main(){
int n;
while(scanf("%d",&n)==){
int netmask[]={};
int ip[]={};
int max,min;
for(int i=;i<n;i++){
scanf("%d.%d.%d.%d",&s[i][],&s[i][],&s[i][],&s[i][]);
}
int ok=,same=;
int k;
for(k=;k<;k++){
max=s[][k],min=s[][k];
for(int i=;i<n;i++){
if(i&&ok&&s[i][k]!=s[i-][k]){
ok=;same=k;
}
if(s[i][k]>max)max=s[i][k];
else if(s[i][k]<min)min=s[i][k];
}
if(!ok)break;
ip[k]=s[][k];
netmask[k]=;
}
int a[]={};
int c=max^min,sum1=,sum2=,sp=;
for(int i=;i<;i++){
if(c>=(<<i)&&c<(<<(i+))){
sp=i+;break;
}
}
getBit(a,min);
for(int i=;i<-sp;i++){
sum1=sum1+(a[i]*(<<(-i)));
sum2+=(*(<<(-i)));
}
ip[k]=sum1;
netmask[k]=sum2;
printf("%d.%d.%d.%d\n",ip[],ip[],ip[],ip[]);
printf("%d.%d.%d.%d\n",netmask[],netmask[],netmask[],netmask[]);
}
return ;
}

Uva 1590 IP Networks的更多相关文章

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

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

  2. UVA 1590 IP Networks JAVA

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

  3. UVa 1590 IP网络(简单位运算)

    Description   Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...

  4. IP Networks UVA - 1590

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

  5. 位运算基础(Uva 1590,Uva 509题解)

    逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...

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

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

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

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

  9. UVa 10253 - Series-Parallel Networks

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. Oracle之视图

    Oracle之视图 2018.9.12 由于视图的数据与表数据互相关联,所以切记谨慎操作 建立视图 使用下面sql语句来完成视图的创建 create or replace view 视图名 as se ...

  2. 数论(一)LOJ1282

    1.题目来源LOJ1282 You are given two integers: n and k, your task is to find the most significant three d ...

  3. 【TOJ 1449】Area of Circles II(求不同位置的两圆面积之和)

    描述 There are two circles on the plane. Now you must to calculate the area which they cover the plane ...

  4. SD 信贷出口 备忘

    信贷出口LVKMPFZ1,LVKMPFZ2,LVKMPFZ3

  5. 构建高可靠hadoop集群之5-服务级别授权

    本人翻译自: http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-common/ServiceLevelAuth.html ...

  6. LVS-DR模式实现调度负载

    本篇文章主要梳理一下LVS前端调度过程及用户请求过程 实验架构 准备工作 添加各主机路由联通主机通信 Client IP route add default gw 172.20.17.19 Route ...

  7. android xml实现animation 4种动画效果

    animation有四种动画类型 分别为alpha(透明的渐变).rotate(旋转).scale(尺寸伸缩).translate(移动),二实现的分发有两种,一种是javaCode,另外一种是XML ...

  8. PHP学习day1

    PHP 变量规则: 变量以 $ 符号开头,其后是变量的名称 变量名称必须以字母或下划线开头 变量名称不能以数字开头 变量名称只能包含字母数字字符和下划线(A-z.0-9 以及 _) 变量名称对大小写敏 ...

  9. Centos6 Ruby 1.8.7升级至Ruby 2.3.1的方法

    本文章地址:https://www.cnblogs.com/erbiao/p/9117018.html#现在的版本 [root@hd4 /]# ruby --version ruby (-- patc ...

  10. [转]App离线本地存储方案

    App离线本地存储方案 原文地址:http://ask.dcloud.net.cn/article/166 HTML5+的离线本地存储有如下多种方案:HTML5标准方案:cookie.localsto ...