子网掩码: 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;
}