1354 - IP Checking
Description
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地址的二进制形式和十进制形式是否相同?
所以我们要检索字符串的每一部分是否相同。这要靠查到对字符串的处理,我这块太差了,以前做大数问题就吃过亏,要多加练习!
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
int t,tt=0;;
char a[105],b[105];
cin>>t;
while(t--)
{
tt++;
scanf("%s%s",a,b);
int f[105]={0},g[105]={0};
int len1=strlen(a),len2=strlen(b);
int j=1;
for(int i=len1-1;i>=0;i--)
{
int dp=1,flag=0;
while(a[i]!='.'&&i>=0)
{
f[j]+=(a[i]-'0')*dp;//依次将两点间的字母转化为数字
dp*=10;
i--;
flag=1;
}
if(flag)
j++;
}
j=1;
for(int i=len2-1;i>=0;i--)
{
int dp=1,flag=0;
while(b[i]!='.'&&i>=0)
{
g[j]+=(b[i]-'0')*dp;//依次将两点间的字母转化为数字
dp*=2;
i--;
flag=1;
}
if(flag)
j++;
}
int out=0;//四个数字中有一个不相同就跳出
for(int i=1;i<=4;i++)//从一开始的啊哦
{
if(f[i]!=g[i])
{
out=1;
break;
}
}
if(out)
cout<<"Case "<<tt<<": "<<"No"<<endl;
else
cout<<"Case "<<tt<<": "<<"Yes"<<endl;
}
return 0;
}