UVA 1590 IP Networks 简单模拟

        WA了许多次,也不知道什么原因。。。。最后拿到官方数据对比发现原来是下溢的问题。。。。-1在无符号数下发生下溢,我仅仅考虑了n的下溢,但是没有考虑到其他的问题。。。确实不应该。

        感觉题意的话.....恩,看样例可以理解题目是要干一件什么事情,但是按照题目的说法,当n=32的时候,最小的IP network岂不是0.0.0.0,

        使用了一些位运算,可能导致看不清楚。以及另外一点就是cin>>和cin.get(c)的返回值都是cin,因此可以连续调用,可能有点丑了。。

        这道题毕竟是一个水题,还要了这么久的时间。。假如说要数据的话,在官网可以下到http://neerc.ifmo.ru/archive/2005.html 点击Test and jury solution,假如还是不会的话,私信我好了,我发给你。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

unsigned m,arr[1010],te[4],n=-1,cmp(int i,int j);
char c;

int main(){
    ios_base::sync_with_stdio(false);
    while(cin>>m){
        for(int i=0;i<m;++i)
            ((((((cin>>te[0]).get(c))>>te[1]).get(c))>>te[2]).get(c))>>te[3],arr[i]=(te[0]<<24)+(te[1]<<16)+(te[2]<<8)+te[3];
        for(unsigned i=0,k,j;i<m;++i)
        for(j=i+1,k=cmp(i,j);j<m;k=cmp(i,++j))
        if(n==-1)
            n=k;
        else if(k!=-1)
            n=max(n,k);


        if(n==-1)
            cout<<(arr[0]>>24)<<c<<((arr[0]&0x00ff0000)>>16)<<c<<((arr[0]&0x0000ff00)>>8)<<c<<(arr[0]&0x000000ff)<<endl<<"255.255.255.255\n";
        else{
            unsigned tem=arr[0];
            for(int i=n;i>=0;--i)
                tem&=(0xffffffff^(1<<i));
            cout<<(tem>>24)<<c<<((tem&0x00ff0000)>>16)<<c<<((tem&0x0000ff00)>>8)<<c<<(tem&0x000000ff)<<endl;
            tem=0xffffffff;
            for(int i=n;i>=0;--i)
                tem^=(1<<i);
            cout<<(tem>>24)<<c<<((tem&0x00ff0000)>>16)<<c<<((tem&0x0000ff00)>>8)<<c<<(tem&0x000000ff)<<endl;
        }
        n=-1;
    }
    return 0;
}

unsigned cmp(int i,int j){
    unsigned tem=arr[i]^arr[j],ans=-1;
    for(unsigned k=0;tem;++k,tem>>=1)
    if(tem&1)
        ans=k;
    return ans;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值