1590 - IP Networks (UVA)

题目链接如下:

Online Judge

学习了bitset,很强大。我的代码如下:

cpp 复制代码
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
// #define debug

int m;
int byte[4], mask[4];
std::vector<std::string> vec;

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while(scanf("%d", &m) == 1){
        vec.clear();
        while(m--){
            std::string s;
            for(int i = 0; i < 4; ++i){
                scanf("%d", &byte[i]);
                std::bitset<8> bitset1(byte[i]);
                s += bitset1.to_string();
                getchar();
            }
            vec.push_back(s);
        }
        int k = 32;
        for(int i = 0; i < 32; ++i){
            for(int j = 1; j < vec.size(); ++j){
                if(vec[j][i] != vec[0][i]){
                    k = i;
                    i = 32;
                    break;
                }
            }
        }
        for(int i = 0; i < 4; ++i){
            byte[i] = 0;
            mask[i] = 0;
            for(int j = 0; j < 8; ++j){
                if(i * 8 + j < k){
                    byte[i] = 2 * byte[i] + vec[0][i * 8 + j] - '0';
                    mask[i] = 2 * mask[i] + 1;
                } else{
                    byte[i] = 2 * byte[i];
                    mask[i] = 2 * mask[i];
                }
            }
            printf("%d%s", byte[i], i == 3 ? "\n" : ".");
        }
        for(int i = 0; i < 4; ++i){
            printf("%d%s", mask[i], i == 3 ? "\n" : ".");
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
say_fall1 分钟前
泛型编程基石:C++ 模板从入门到熟练
java·开发语言·c++·编辑器·visual studio
txinyu的博客7 分钟前
结合游戏场景解析UDP可靠性问题
java·开发语言·c++·网络协议·游戏·udp
郝学胜-神的一滴13 分钟前
深入解析Mipmap层级判定原理:从理论到实践
c++·unity·godot·游戏程序·图形渲染·unreal engine
雾岛听蓝13 分钟前
探索C++继承机制
开发语言·c++
名字不好奇28 分钟前
在C++中 如何实现java中的Stream
java·c++
喵星人工作室32 分钟前
C++传说:神明之剑0.2.1
开发语言·c++·游戏
紫色的路1 小时前
TCP消息边界处理的精妙算法
c++·网络协议·tcp/ip·算法
chamu991 小时前
C++ 的可调用对象
开发语言·c++
千里马-horse1 小时前
Drawing a triangle -- setup -- Base code
c++·vulcan
txinyu的博客1 小时前
unique_ptr shared_ptr weak_ptr的线程安全问题
c++·安全