UVa1590

#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
#include<map> 
#include<set>
#include<sstream>
#include<cassert>
#include<string.h>

using namespace std;
const int INF = 0x3f3f3f3f;
string network[1005];

string ttb(const int val)
{
    string ss="";
    for (int i = 7; i >= 0; i--)
    {
        if (val & (1 << i))
            ss=ss + "1";
        else
            ss=ss + "0";
    }
    return ss;
}

int main()
{
    int m;
    string temp1[4],sum;
    int tmp1, tmp2, tmp3, tmp4;
    while (cin >> m)
    {
        int k = 0;
        while (m--)
        {
            cin >> network[k++];
        }
        for (int i = 0;i < k;i++)
        {
            int temp, start = 0;
            int len = 32;
            for (int j = 0;j < 4;j++)
            {
                temp = network[i].substr(start, len).find('.');
                temp1[j] = network[i].substr(start,temp);
                start += (temp + 1);
                len=network[i].size() - start;
            }
            tmp1 = stoi(temp1[0]);
            tmp2 = stoi(temp1[1]);
            tmp3 = stoi(temp1[2]);
            tmp4 = stoi(temp1[3]);
            temp1[0] = ttb(tmp1);
            temp1[1] = ttb(tmp2);
            temp1[2] = ttb(tmp3);
            temp1[3] = ttb(tmp4);
            network[i] = temp1[0]+ temp1[1]+ temp1[2] + temp1[3];
        }
        int flag = 1;
        int pos = 32;
        for (int i = 0;i < 32;i++)
        {
            for (int j = 1;j < k;j++)
            {
                if (network[0][i]!= network[j][i])
                {
                    pos = i;
                    flag = 0;
                    break;
                }
            }
            if (!flag)
                break;
        }
        for (int j=pos;j<32;j++)
        {
            network[0][j] = '0';
        }
        temp1[0] = network[0].substr(0, 8);
        temp1[1] = network[0].substr(8, 8);
        temp1[2] = network[0].substr(16, 8);
        temp1[3] = network[0].substr(24, 8);
        tmp1 = tmp2 = tmp3 = tmp4 = 0;
        for (int i = 0;i <=7;i++)
        {
            tmp1 += (temp1[0][i]-'0') *(int )(pow(2, 7-i)+0.1);
            tmp2 += (temp1[1][i] - '0') *(int)(pow(2, 7-i) + 0.1);
            tmp3 += (temp1[2][i] - '0') *(int)(pow(2,7- i) + 0.1);
            tmp4 += (temp1[3][i] - '0') *(int)(pow(2, 7-i) + 0.1);
        }
        cout << tmp1 << "." << tmp2 << "." << tmp3 << "." << tmp4 << endl;
        sum = "";
        for (int i = 0;i < pos;i++)
        {
            sum = sum + "1";
        }
        for (int i = pos;i < 32;i++)
        {
            sum = sum + "0";
        }
        temp1[0] = sum.substr(0, 8);
        temp1[1] = sum.substr(8, 8);
        temp1[2] = sum.substr(16, 8);
        temp1[3] = sum.substr(24, 8);
        tmp1 = tmp2 = tmp3 = tmp4 = 0;
        for (int i = 0;i <= 7;i++)
        {
            tmp1 += (temp1[0][i] - '0') *(int)(pow(2, 7 - i) + 0.1);
            tmp2 += (temp1[1][i] - '0') *(int)(pow(2, 7 - i) + 0.1);
            tmp3 += (temp1[2][i] - '0') *(int)(pow(2, 7 - i) + 0.1);
            tmp4 += (temp1[3][i] - '0') *(int)(pow(2, 7 - i) + 0.1);
        }
        cout << tmp1 << "." << tmp2 << "." << tmp3 << "." << tmp4 << endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值