• 【习题 4-5 Uva1590】 IP Networks


    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    假设从第i位开始有不一样的。 那么就把i+1..32位全都置0. 掩码的话类似。前i为全为1,后面32-i位全0. 尽量让后面的连续0(等于掩码最后的0的个数)晚一点出现。 这样网络表示的范围比较小(可供变化的范围比较小)。

    【代码】

    #include <bits/stdc++.h>
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    using namespace std;
    
    const int N = 1e3;
    
    int n;
    string ips[N+10];
    
    string get_format(string s){
        string temp = "";
        int x = 0;
        rep1(i,1,32){
            x = x*2+(s[i-1]-'0');
            if (i%8==0){
                string temp1 = "";
                if (x==0) temp1 = "0";
                while (x>0){
                    temp1 =(char)(x%10+'0')+temp1;
                    x/=10;
                }
                temp = temp + temp1;
                if (i<32) temp+=".";
            }
        }
        return temp;
    }
    
    int main(){
        //freopen("/home/ccy/rush.txt","r",stdin);
       // freopen("/home/ccy/rush_out.txt","w",stdout);
        ios::sync_with_stdio(0),cin.tie(0);
        while (cin >> n){
            rep1(i,1,n){
                ips[i]="";
                string s;
                cin >> s;s+=".";
                int x= 0;
                rep1(j,0,(int)s.size()-1){
                    if (s[j]=='.'){
                        string temp = "";
                        rep1(k,1,8){
                            int temp1 = x%2;
                            temp = (char)(temp1+'0')+temp;
                            x/=2;
                        }
                        ips[i]+=temp;
                        x = 0;
                    }else{
                        x = x*10+s[j]-'0';
                    }
                }
                //cout<<ips[i]<<endl;
            }
            string ans1 = "";
            string ans2 = "";
            for (int i = 0;i < 32;i++){
                int ok = 1;
                rep1(j,1,n)
                    if (ips[j][i]!=ips[1][i])
                        ok = 0;
                if (ok){
                    ans1 = ans1 + ips[1][i];
                    ans2 = ans2 + "1";
                }else{
                    rep1(j,i,31) {
                        ans1+="0";
                        ans2+="0";
                    }
                    break;
                }
            }
            //cout<<ans1<<endl;
            //cout<<ans2<<endl;
            cout<<get_format(ans1)<<endl;
            cout<<get_format(ans2)<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    负环判断模版
    计蒜客NOIP2017提高组模拟赛(四)day1
    NOIP2014-10-30模拟赛
    树链剖分模版
    2014-10-30NOIP复习题1
    NOIP2014-5-17模拟赛
    NOIP2014-5-10模拟赛
    NOIP2014-3-15模拟赛
    HDU1556Color the ball
    HDU1166敌兵布阵
  • 原文地址:https://www.cnblogs.com/AWCXV/p/9874744.html
Copyright © 2020-2023  润新知