lightoj-1354 - IP Checking(水题)

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\n":"No\n");
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\n":"No\n");
79 //    }
80 //    
81 //    return 0;
82 //} 

 

转载于:https://www.cnblogs.com/yuanshixingdan/p/5564038.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值