您的位置:首页 > 其它

lightOJ 1354 - IP Checking 【字符串处理&&进制转化】

2015-11-08 19:10 302 查看
1354 - IP Checking



PDF (English)StatisticsForum
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

PROBLEM SETTER: JANE ALAM JAN

思路:

直接将用点(.)分开的数都计算出来,然后保存到两个数组中,然后进行比较就行了,如果都相等,就输出OK!否则输出NO!

代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char a[50],b[50];
int c[5],d[5];
int len1,len2;
int main()
{
int T;
scanf("%d",&T);
int N=T;
getchar();
while(T--)
{
scanf("%s%s",a,b);
len1=strlen(a);
len2=strlen(b);
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
int t=0;
for(int i=0;i<len1;i++)
{
if(a[i]!='.')
{
c[t]=c[t]*10+a[i]-'0';
}
else
{
t++;
}
}
t=0;
for(int i=0;i<len2;i++)
{
if(b[i]!='.')
{
d[t]=d[t]*2+b[i]-'0';
}
else
{
t++;
}
}
int i;
for(i=0;i<4;i++)
{
if(c[i]!=d[i])
break;
}
printf("Case %d: ",N-T);
if(i!=4)
{
printf("No\n");
}
else
{
printf("Yes\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: