2015百度之星资格赛 IP聚合 1003

前端之家收集整理的这篇文章主要介绍了2015百度之星资格赛 IP聚合 1003前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

IP聚合

Accepts: 2354
Submissions: 6308
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个网络地址。网络地址等于子网掩码与 IP 地址按位进行与运算后的结果,例如: 子网掩码:A.B.C.D IP 地址:a.b.c.d 网络地址:(A&a).(B&b).(C&c).(D&d)
Input
第一行包含一个整数 T 1T50 代表测试数据的组数, 接下来 T 组测试数据。每组测试数据包含若干行, 第一行两个正整数 N1N1000,1M50,M 。接下来 N 行,每行一个字符串,代表一个 IP 地址, 再接下来 M 行,每行一个字符串代表子网掩码。IP 地址和子网掩码均采用 A.B.C.D 的形式,其中 AB@H_835_301@CD 均为非负整数,且小于等于255。
Output
对于每组测试数据,输出两行: 第一行输出: "Case #i:" 。 i 代表第 i 组测试数据。 第二行输出测试数据的结果,对于每组数据中的每一个子网掩码,输出在此子网掩码下的网络地址的数量
Sample Input
  1. 2
  2. 5 2
  3. 192.168.1.0
  4. 192.168.1.101
  5. 192.168.2.5
  6. 192.168.2.7
  7. 202.14.27.235
  8. 255.255.255.0
  9. 255.255.0.0
  10. 4 2
  11. 127.127.0.1
  12. 10.134.52.0
  13. 127.0.10.1
  14. 10.134.0.2
  15. 235.235.0.0
  16. 1.57.16.0
Sample Output
  1. Case #1:
  2. 3
  3. 2
  4. Case #2:
  5. 3
想用优先队列,但是没写出来。其实直接模拟就可以了。。数组开小wa了几次。。
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #include<algorithm>
  5. #include<stack>
  6. using namespace std;
  7. int a[1010],b[1010],c[1010],d[1010];
  8. int p1[1010],p2[1010],p3[1010],p4[1010];
  9. int main()
  10. {
  11. int t;
  12. int res[1100];
  13. scanf("%d",&t);
  14. int num=1;
  15. while(t--){
  16. memset(res,sizeof(res));
  17. int n,m;
  18. scanf("%d%d",&n,&m);
  19. for(int i=0;i<n;i++)
  20. scanf("%d.%d.%d.%d",&a[i],&b[i],&c[i],&d[i]);
  21. int A,B,C,D;
  22. for(int i=0;i<m;i++){
  23. scanf("%d.%d.%d.%d",&A,&B,&C,&D);
  24. int t1,t2,t3,t4;
  25. int tot=1;
  26. for(int j=0;j<n;j++){
  27. int x=0;
  28. t1=A&a[j],t2=B&b[j],t3=C&c[j],t4=D&d[j];
  29. if(j==0){
  30. p1[1]=t1,p2[1]=t2,p3[1]=t3,p4[1]=t4;
  31. continue;
  32. }
  33. for(int k=1;k<=tot;k++){
  34. if(t1==p1[k]&&t2==p2[k]&&t3==p3[k]&&t4==p4[k])
  35. x=1;
  36. }
  37. if(!x){
  38. tot++;
  39. p1[tot]=t1,p2[tot]=t2,p3[tot]=t3,p4[tot]=t4;
  40. }
  41. }
  42. res[i]=tot;
  43. }
  44. printf("Case #%d:\n",num++);
  45. for(int i=0;i<m;i++)
  46. printf("%d\n",res[i]);
  47. }
  48. return 0;
  49. }

猜你在找的设计模式相关文章