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
代码
又是一道水题,数据范围这么小。。。
- #include <iostream>
- #include <cstdio>
- #include <cstring>
-
- using namespace std;
-
- int main()
- {
- int t;
- int n,m;
- int a,b,c,d,s[1010][4],f[1010][4],cnt;
- scanf("%d",&t);
- for(int tt = 1; tt <= t; tt++)
- {
- scanf("%d%d",&n,&m);
- for(int i = 0; i < n; i++)
- {
- scanf("%d.%d.%d.%d",&s[i][0],&s[i][1],&s[i][2],&s[i][3]);
- }
- printf("Case #%d:\n",tt);
- while(m--)
- {
- cnt = 0;
- int ans = 0;
- scanf("%d.%d.%d.%d",&a,&b,&c,&d);
- for(int i = 0; i < n; i++)
- {
- int aa = a & s[i][0],bb = b & s[i][1],cc = c & s[i][2],dd = d & s[i][3];
- bool flag = 0;
- for(int j = 0; j < cnt; j++)
- {
- if(f[j][0] == aa && f[j][1] == bb && f[j][2] == cc && f[j][3] == dd)
- {
- flag = 1;
- break;
- }
- }
- if(!flag)
- {
- ans++;
- f[cnt][0] = aa;
- f[cnt][1] = bb;
- f[cnt][2] = cc;
- f[cnt][3] = dd;
- cnt++;
- }
- }
- printf("%d\n",ans);
- }
- }
- return 0;
- }