/* 有时间日后应该重做***! 因为做这道题的时候,还没有学到计算机网络,查了许久关于网络地址和掩码的资料,但总觉得对这个概念并不算太清晰,所以只能暂且先用别人的思路写一次,日后学到应该重做! 代码借鉴了: http://blog.csdn.net/ronggerace/article/details/42346413 这个blog的解析较为详细: http://blog.csdn.net/hahaac/article/details/52649997 */
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int M = 10005; int a[M][32]; int main() { int T, m, b[32], c[32]; // b、c分别为网络地址和子网掩码的32位二进制 while (cin >> T) { for (int i = 0; i < T; i++) { for (int j = 0; j < 4; j++) { cin >> m; getchar(); //. for (int k = 7; k >= 0; k--) a[i][8 * j + 7 - k] = (m >> k) % 2; } } int mid = -1, i, j; for (i = 0; i < 32; i++) //遍历各位,找到第一个不同的位置 for (j = 1; j < T; j++) if (a[0][i] != a[j][i]) { mid = i; j = T; i = 32; //退出两层循环 } if (mid == -1) mid = 32; for (i = 0; i < mid; i++) { b[i] = a[0][i]; c[i] = 1; } for (i = mid; i < 32; i++) { b[i] = c[i] = 0; } int p[4], q[4]; memset(p, 0, sizeof(p)); memset(q, 0, sizeof(q)); for (i = 0; i < 4; i++) for (j = 0; j < 8; j++) { p[i] = p[i] * 2 + b[8 * i + j]; q[i] = q[i] * 2 + c[8 * i + j]; } char ch = '.'; cout << p[0] << ch << p[1] << ch << p[2] << ch << p[3] << endl; cout << q[0] << ch << q[1] << ch << q[2] << ch << q[3] << endl; } return 0; }