题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
#include <stdio.h> int main() { int ip[3][4] = {0}; for (int i = 0; i < 3; i++) { scanf("%d.%d.%d.%d", &ip[i][0], &ip[i][1], &ip[i][2], &ip[i][3]); } if (ip[0][0] != 255 || ip[0][3] != 0) { printf("1"); return 0; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { if (ip[i][j] > 255 || ip[i][j] < 0) { printf("1"); return 0; } } } int flag = 0; for (int j = 0; j < 4; j++){ if ((ip[0][j]&ip[1][j])!=(ip[0][j]&ip[2][j])) { printf("2"); return 0; } } printf("0"); return 0; }