题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
当然要拍照记录啊
# include <iostream> # include <string> # include <vector> using namespace std; bool realm(int mask[4]) { int tag = 1, tag1 = 0; int a[9] = { 128,192,224,240,248,252,254,255,0 }; for (int i = 0; i < 4; i++) { for (int j = 0; j < 9; j++) { if (a[j] == mask[i]) tag1 = 1; } if (tag == 0 && mask[i] != 0 || tag1 == 0) return false; if (mask[i] != 255) tag = 0; tag1 = 0; } return true; } bool realip(int ip[4]) { for (int i = 0; i < 4; i++) { if (ip[i] < 0 || ip[i]>255) return false; } return true; } int main() { int mask[4], ip1[4], ip2[4]; char c; while (cin >> mask[0] >> c >> mask[1] >> c >> mask[2] >> c >> mask[3]) { int tag = 0; for (int i = 0; i < 4; i++) { cin >> ip1[i]; if (i < 3) cin >> c; } for (int i = 0; i < 4; i++) { cin >> ip2[i]; if (i < 3) cin >> c; } if (!realm(mask) || !realip(ip1) || !realip(ip2)) { cout << 1 << endl; continue; } for (int i = 0; i < 4; i++) { if ((ip1[i] & mask[i]) != (ip2[i] & mask[i])) tag = 2; } cout << tag << endl; } return 0; }