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;
}
相关推荐
2301_7779983413 小时前
Linux线程同步与互斥(三):信号量与环形队列实现生产者消费者模型
linux·c语言·c++
啦啦啦啦啦zzzz13 小时前
设计模式:单例模式和工厂模式
c++·单例模式·设计模式·工厂模式
Henry Zhu12313 小时前
C++ 动态多态详解
c++
此生决int13 小时前
深入理解C++系列(06)——模版初阶与STL
开发语言·c++
c2385614 小时前
《枚举算法 “翻译官”:用 enum class 给 “游游的礼盒” 算分》
c语言·c++·算法
noipp14 小时前
推荐题目:洛谷 P5843 [SCOI2012] Blinker 的噩梦
c语言·数据结构·c++·算法·游戏·洛谷·luogu
鱼子星_14 小时前
《算 · 法》题目解析篇(1):最近公共祖先问题,线段树,最长回文子序列
c++·算法·动态规划·递归
计算机内卷的N天16 小时前
CMake与Visual Studio的使用
c++·ide·visual studio
汉克老师16 小时前
GESP2026年3月认证C++七级( 第三部分编程题(1、物流网络))精讲
c++·最短路·gesp7级
鱼子星_16 小时前
【C++】深入剖析list:list及其双向迭代器实现
开发语言·数据结构·c++·笔记·stl·list