O

来源:互联网 发布:淘宝生日礼包在哪领取 编辑:程序博客网 时间:2024/04/29 09:06

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

    2

    192.168.0.100

    11000000.10101000.00000000.11001000

    65.254.63.122

    01000001.11111110.00111111.01111010
Sample Output

    Case 1: No

    Case 2: Yes

    题意:给出两个IP地址,一个是十进制形式,另一个是二进制形式,问这两个IP地址是否相同

    思路;直接把二进制的数化为十进制就好了,如果四个数都相同就输出yes,否则No;(一开始用字符串写的。。很麻烦)

   下面附上代码:

   

#include<bits/stdc++.h>using namespace std;char s[8];int main(){int a,b,c,d,t,k=0,i,j;int x,y,z,l;cin>>t;while(t--){int sum1=0,sum2=0,sum3=0,sum4=0;int flag1=0,flag2=0,flag3=0,flag4=0;scanf("%d.%d.%d.%d",&a,&b,&c,&d);scanf("%d.%d.%d.%d",&x,&y,&z,&l);i=0;while(x){sum1+=pow(2,i)*(x%10);i++;x/=10;}if(sum1==a) flag1=1;i=0;while(y){sum2+=pow(2,i)*(y%10);i++;y/=10;}if(sum2==b) flag2=1;i=0;while(z){sum3+=pow(2,i)*(z%10);i++;z/=10;}if(sum3==c) flag3=1;i=0;while(l){sum4+=pow(2,i)*(l%10);i++;l/=10;}if(sum4==d) flag4=1;printf("Case %d: ",++k);if(flag1&&flag2&&flag3&&flag4) puts("Yes");else puts("No");}return 0;}







原创粉丝点击