当前位置: 代码迷 >> 综合 >> 百度程序设计大赛1003IP聚合
  详细解决方案

百度程序设计大赛1003IP聚合

热度:91   发布时间:2024-01-16 00:52:14.0

主要没什么,只是涉及到了一个结构体的排序就OK了。

唯一的问题就是提交的时候WA了3次,还以为是算法错了,结果数组开到1e5就过了!!!

测试数据居然比给的范围要大!!!

测试数据居然比给的范围要大!!!

测试数据居然比给的范围要大!!!

(关键问题说3遍)

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;struct node{int a;int b;int c;int d;
}a[10550],b[2000],ans[2000],temp;bool cmp(node x, node y){if(x.a != y.a)  return x.a<=y.a;if(x.b != y.b)  return x.b<=y.b;if(x.c != y.c)  return x.c<=y.c;if(x.d != y.d)  return x.d<=y.d;
}int main(){int T;int cnt;//freopen("in.txt","r",stdin);scanf(" %d",&T);for(int x = 1;x <= T;x++){int N,M;scanf(" %d %d",&N,&M);for(int j = 0;j < N;j++)scanf(" %d.%d.%d.%d",&a[j].a,&a[j].b,&a[j].c,&a[j].d);//IP地址for(int i = 0;i < M;i++)scanf(" %d.%d.%d.%d",&b[i].a,&b[i].b,&b[i].c,&b[i].d);//子网掩码printf("Case #%d:\n",x);for(int i = 0;i < M;i++){for(int j = 0;j < N;j++){ans[j].a = a[j].a&b[i].a;ans[j].b = a[j].b&b[i].b;ans[j].c = a[j].c&b[i].c;ans[j].d = a[j].d&b[i].d;}sort(ans,ans+N,cmp);cnt = N;for(int j = 1;j < N;j++){temp = ans[j-1];if(temp.a == ans[j].a && temp.b == ans[j].b && temp.c == ans[j].c && temp.d == ans[j].d)cnt--;}printf("%d\n",cnt);}}return 0;
}

  相关解决方案