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 |
分析:
这道题让我知道了很多知识,比如说二进制转换成十进制,字符串如何转换成数字。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ip1[110],ip2[110];
int aa[5],bb[5];
int main()
{
int t;
int cnt=0;
scanf("%d",&t);
while(t--)
{
scanf("%s",ip1);
scanf("%s",ip2);
int l1=strlen(ip1);
int l2=strlen(ip2);
int k=0,temp=0;
for(int i=0;i<l1;i++)
{
if(ip1[i]=='.')
{
aa[k++]=temp;
temp=0;
}
else
temp=temp*10+ip1[i]-'0';
}
aa[k++]=temp;
k=0,temp=0;
for(int i=0;i<l2;i++)
{
if(ip2[i]=='.')
{
bb[k++]=temp;
temp=0;
}
else
temp=temp*2+ip2[i]-'0';
}
bb[k++]=temp;
int flag=1;
for(int i=0;i<4;i++)
{
if(aa[i]!=bb[i])
{
flag=0;
break;
}
}
if(flag)
printf("Case #%d: Yes\n",++cnt);
else
printf("Case #%d: No\n",++cnt);
}
return 0;
}