题目意思很清楚,只要使用按位与的的方法,计算出相应结果就可以了。。
水题一道,可以练练常用的数据结构...
#include <cstdio> #include <vector> #include <set> #include <iostream> #include <cstring> using namespace std; const int MAXN = 1005; struct IntNode{int a,b,c,d;}; IntNode ym[MAXN]; //掩码 IntNode ip[MAXN]; //ip地址 set<long long> res; int T,N,M; int cas=1; long long calculate(int i,int j) { long long tmpa,tmpb,tmpc,tmpd,sum; tmpa = ym[i].a & ip[j].a; tmpb = ym[i].b & ip[j].b; tmpc = ym[i].c & ip[j].c; tmpd = ym[i].d & ip[j].d; sum = tmpd + tmpc * 1000 + tmpb * 1000000 + tmpa * 1000000000; return sum; } int main() { cin>>T; while(cin>>N>>M) { memset(ym,sizeof(ym)); memset(ip,sizeof(ip)); for(int i = 1;i <= N; i++) scanf("%d.%d.%d.%d",&ip[i].a,&ip[i].b,&ip[i].c,&ip[i].d); for(int i = 1;i <= M; i++) scanf("%d.%d.%d.%d",&ym[i].a,&ym[i].b,&ym[i].c,&ym[i].d); printf("Case #%d:\n",cas++); for (int i = 1; i <= M; i++) { res.clear(); for(int j = 1; j <= N; j++) { long long num = calculate(i,j); if (!res.count(num)) res.insert(num); } printf("%d\n",res.size()); } } return 0; }原文链接:/javaschema/284738.html