IP聚合

子网掩码: A.B.C.D

IP地址:a.b.c.d

网络地址:(A&a).(B&b).(C&c).(D&d)

问在固定的子网掩码下有多少个网络地址。

#include <cstdio>
#include <set>

using namespace std;

struct rec
{
    int a, b, c, d;
    
    bool operator <(const rec &s) const
    {
        if (this->a == s.a && this->b == s.b && this->c == s.c)
            return this->d < s.d;
        else if (this->a == s.a && this->b == s.b)
            return this->c < s.c;
        else if (this->a == s.a)
            return this->b < s.b;
        else
            return this->a < s.a;
    }
};

set<rec> s;

const int MAXN = 1000;
const int MAXM = 50;

rec ip[MAXN];
rec sm[MAXM];

int main()
{
    int t;
    scanf("%d", &t);
    
    for (int i = 1; i <= t; i++) {
        printf("Case #%d:\n", i);
        
        int n, m;
        scanf("%d%d", &n, &m);
        
        for (int j = 0; j < n; j++) {
            scanf("%d.%d.%d.%d", &ip[j].a, &ip[j].b, &ip[j].c, &ip[j].d);
        }
        for (int j = 0; j < m; j++) {
            scanf("%d.%d.%d.%d", &sm[j].a, &sm[j].b, &sm[j].c, &sm[j].d);
        }
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < n; k++) {
                rec tmp;
                tmp.a = sm[j].a & ip[k].a;
                tmp.b = sm[j].b & ip[k].b;
                tmp.c = sm[j].c & ip[k].c;
                tmp.d = sm[j].d & ip[k].d;
                s.insert(tmp);
            }
            printf("%lu\n", s.size());
            s.clear();
        }
    }
    
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值