IP聚合

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

Problem Description

当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个网络地址。网络地址等于子网掩码与 IP 地址按位进行与运算后的结果,例如:

子网掩码:A.B.C.D

IP 地址:a.b.c.d

网络地址:(A&a).(B&b).(C&c).(D&d)

Input

第一行包含一个整数T,(1≤T≤50)代表测试数据的组数,

接下来T组测试数据。每组测试数据包含若干行,

第一行两个正整数N(1≤N≤1000,1≤M≤50),M。接下来N行,每行一个字符串,代表一个 IP 地址,

再接下来M行,每行一个字符串代表子网掩码。IP 地址和子网掩码均采用 A.B.C.D的形式,其中A,B,C,D均为非负整数,且小于等于255。

Output

对于每组测试数据,输出两行:

第一行输出: “Case #i:” 。i代表第i组测试数据。

第二行输出测试数据的结果,对于每组数据中的每一个子网掩码,输出在此子网掩码下的网络地址的数量

Sample Input

2
5 2
192.168.1.0
192.168.1.101
192.168.2.5
192.168.2.7
202.14.27.235
255.255.255.0
255.255.0.0
4 2
127.127.0.1
10.134.52.0
127.0.10.1
10.134.0.2
235.235.0.0
1.57.16.0

Sample Output

Case #1:
3
2
Case #2:
3
4

代码

又是一道水题,数据范围这么小。。。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int t;
  10. int n,m;
  11. int a,b,c,d,s[1010][4],f[1010][4],cnt;
  12. scanf("%d",&t);
  13. for(int tt = 1; tt <= t; tt++)
  14. {
  15. scanf("%d%d",&n,&m);
  16. for(int i = 0; i < n; i++)
  17. {
  18. scanf("%d.%d.%d.%d",&s[i][0],&s[i][1],&s[i][2],&s[i][3]);
  19. }
  20. printf("Case #%d:\n",tt);
  21. while(m--)
  22. {
  23. cnt = 0;
  24. int ans = 0;
  25. scanf("%d.%d.%d.%d",&a,&b,&c,&d);
  26. for(int i = 0; i < n; i++)
  27. {
  28. int aa = a & s[i][0],bb = b & s[i][1],cc = c & s[i][2],dd = d & s[i][3];
  29. bool flag = 0;
  30. for(int j = 0; j < cnt; j++)
  31. {
  32. if(f[j][0] == aa && f[j][1] == bb && f[j][2] == cc && f[j][3] == dd)
  33. {
  34. flag = 1;
  35. break;
  36. }
  37. }
  38. if(!flag)
  39. {
  40. ans++;
  41. f[cnt][0] = aa;
  42. f[cnt][1] = bb;
  43. f[cnt][2] = cc;
  44. f[cnt][3] = dd;
  45. cnt++;
  46. }
  47. }
  48. printf("%d\n",ans);
  49. }
  50. }
  51. return 0;
  52. }

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