Solution: Subnet Mask

Description Title
subnet mask of the IP address is used to determine whether any two computers according to the same subnetwork.
The most simple understanding is the two computers each IP address and subnet mask AND operation, if the result is the same, then the two computers are in the same sub-network, can direct communication . It's that simple.

Consider the following example:

One of the operational demonstration:
IP address 192.168.0.1
subnet mask of 255.255.255.0

Binary arithmetic conversions:
the IP address 11010000.10101000.00000000.00000001
subnet mask 11111111.11111111.11111111.00000000

AND operation:
     11010000.10101000.00000000.00000000

After the conversion to decimal is:
      192.168.0.0

Operation of the two presentations:
IP address 192.168.0.254
Subnet Mask 255.255.255.0

Binary arithmetic conversions:
the IP address 11010000.10101000.00000000.11111110
subnet mask 11111111.11111111.11111111.00000000

AND operation:
     11010000.10101000.00000000.00000000

After the conversion to decimal is:
      192.168.0.0

Three op demo of:
IP address 192.168.0.4
subnet mask of 255.255.255.0

Binary arithmetic conversions:
the IP address 11010000.10101000.00000000.00000100
subnet mask 11111111.11111111.11111111.00000000

AND operation:
     11010000.10101000.00000000.00000000

After the conversion to decimal is:
      192.168.0.0

By the above AND operation of the three groups of computer IP address and subnet mask, we can see that it is the same as the result of the operation, it is 192.168.0.0, so the computer will be considered as these three computers in the same subnet .
Input
of the first input line is a local IP address;
the second line is the subnet mask;
third line is an integer N, the N IP addresses behind;
Next Line N:
1st IP address
...
...
of IP addresses of the N
outputs
is calculated and output N and the machine IP address is in the same subnet. For output in the same subnet "INNER", the output for different subnets "OUTER".
Sample input
192.168.0.1
255.255.255.0
. 3
192.168.0.2
192.168.0.254
192.168.1.2
sample output from
the INNER
the INNER
OUTER

&: Bitwise AND

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
 int n,a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,d5;
 scanf("%d.%d.%d.%d",&a1,&b1,&c1,&d1);
 scanf("%d.%d.%d.%d",&a2,&b2,&c2,&d2);//
 a3=a1&a2;b3=b1&b2;c3=c1&c2;d3=d1&d2;
 cin>>n;
 while(n--)
 {
 	scanf("%d.%d.%d.%d",&a4,&b4,&c4,&d4);
 	a5=a4&a2;b5=b4&b2;c5=c4&c2;d5=d4&d2;
 	if(a5==a3&&b5==b3&&c5==c3&&d5==d3)
 	 cout<<"INNER"<<endl;
 	else cout<<"OUTER"<<endl;
 }
 return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_43540515/article/details/91872894