您的位置:首页 > 其它

Light OJ 1354 - IP Checking 【二进制转化】

2015-11-08 20:15 316 查看
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
恩,就是让比较两者形式的IP是否一样。简单的二进制转化。

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int s[5][10];
char str[50];
int rec[9]={1,2,4,8,16,32,64,128,256};
int tran(int a[])
{
int s=0;
for(int i=7;i>=0;i--)
s=s+a[i]*rec[7-i];
return s;
}
int main()
{
int t,a,b,c,d,cnt=0;
scanf("%d",&t);
while(t--)
{
scanf("%d.%d.%d.%d",&a,&b,&c,&d);
scanf("%s",str);
int len=strlen(str);
int j=0;
for(int i=0;i<len;++i)
{
int k=0;
while(i<len&&str[i]!='.')
{
s[j][k++]=str[i]-'0';
i++;
}
j++;
}
printf("Case %d: ",++cnt);
int aa=tran(s[0]);
int bb=tran(s[1]);
int cc=tran(s[2]);
int dd=tran(s[3]);
if(aa==a&&bb==b&&cc==c&&dd==d)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: