计算后排序处理一下;
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N = 100005; struct node{ int a1,a2,a3,a4; }ip[N],sn[N],res[N]; bool cmp(node a,node b){//用于排序后决定由多少个网络地址 if(a.a1 != b.a1) return a.a1 <= b.a1; if(a.a2 != b.a2) return a.a2 <= b.a2; if(a.a3 != b.a3) return a.a3 <= b.a3; if(a.a4 != b.a4) return a.a4 <= b.a4; } int main(){ int t,tot = 1,m,n; scanf("%d",&t); while(t--){ memset(res,sizeof(res)); scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) scanf("%d.%d.%d.%d",&ip[i].a1,&ip[i].a2,&ip[i].a3,&ip[i].a4); for(int i = 1; i <= m; i++) scanf("%d.%d.%d.%d",&sn[i].a1,&sn[i].a2,&sn[i].a3,&sn[i].a4); int k = 0; printf("Case #%d:\n",tot++); for(int i = 1; i <= m; i++){ int c = 0,cnt = 0; for(int j = 1; j <= n; j++){ res[k].a1 = ip[j].a1 & sn[i].a1; res[k].a2 = ip[j].a2 & sn[i].a2; res[k].a3 = ip[j].a3 & sn[i].a3; res[k].a4 = ip[j].a4 & sn[i].a4; k++; } sort(res + k - n,res + k,cmp); for(int j = k - n; j < k; j++){ if((res[j].a1 == res[j + 1].a1) && (res[j].a2 == res[j + 1].a2) && (res[j].a3 == res[j + 1].a3) && (res[j].a4 == res[j + 1].a4)) c++; } printf("%d\n",n - c); } } return 0; }原文链接:https://www.f2er.com/javaschema/284736.html