lightoj-1354

1354 - IP Checking
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
An IP address is a 32 bit address formatted in the following way

a.b.c.d

where a, b, c, d are integers each ranging from 0 to 255. Now you are given two IP addresses, first one in decimal form and second one in binary form, your task is to find if they are same or not.

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with two lines. First line contains an IP address in decimal form, and second line contains an IP address in binary form. In binary form, each of the four parts contains 8 digits. Assume that the given addresses are valid.

Output
For each case, print the case number and "Yes" if they are same, otherwise print "No".

Sample Input
Output for Sample Input
2
192.168.0.100
11000000.10101000.00000000.11001000
65.254.63.122
01000001.11111110.00111111.01111010
Case 1: No
Case 2: Yes

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 int main(){
 7     
 8     int T,a[4],a1[4];
 9     
10     scanf("%d",&T);
11     for(int t=1;t<=T;t++){
12         
13         scanf("%d.%d.%d.%d",&a[0],&a[1],&a[2],&a[3]);
14         scanf("%d.%d.%d.%d",&a1[0],&a1[1],&a1[2],&a1[3]);
15         int flag = 0;
16         for(int j=0;j<4&&flag==0;j++)
17             for(int i=0;i<8;i++){
18                 if((a[j]&1)!=a1[j]%10){
19                     flag = 1;
20                     break;
21                 }
22                 a[j]/=2,a1[j]/=10;
23             }
24 
25         printf("Case %d: ",t);
26         printf(flag==0?"Yes
":"No
");
27     }
28     
29     return 0;
30 } 
31 //未优化
32 //int main(){
33 //    
34 //    int T,a,b,c,d,a1,b1,c1,d1;
35 //    
36 //    scanf("%d",&T);
37 //    for(int t=1;t<=T;t++){
38 //        
39 //        scanf("%d.%d.%d.%d",&a,&b,&c,&d);
40 //        scanf("%d.%d.%d.%d",&a1,&b1,&c1,&d1);
41 //        int flag = 0;
42 //        
43 //        for(int i=0;i<8;i++){
44 //            if((a&1)!=a1%10){
45 //                flag = 1;
46 //                break;
47 //            }
48 //            a/=2,a1/=10;
49 //        }
50 //        if(flag==0){
51 //            for(int i=0;i<8;i++){
52 //                if((b&1)!=b1%10){
53 //                    flag = 1;
54 //                    break;
55 //                }
56 //                b/=2,b1/=10;
57 //            }
58 //        }
59 //        if(flag==0){
60 //            for(int i=0;i<8;i++){
61 //                if((c&1)!=c1%10){
62 //                    flag = 1;
63 //                    break;
64 //                }
65 //                c/=2,c1/=10;
66 //            }
67 //        }
68 //        if(flag==0){
69 //            for(int i=0;i<8;i++){
70 //                if((d&1)!=d1%10){
71 //                    flag = 1;
72 //                    break;
73 //                }
74 //                d/=2,d1/=10;
75 //            }
76 //        }
77 //        printf("Case %d: ",t);
78 //        printf(flag==0?"Yes
":"No
");
79 //    }
80 //    
81 //    return 0;
82 //} 
原文地址:https://www.cnblogs.com/yuanshixingdan/p/5564038.html