Light oj1354:IP Checking

D - 楼上正解
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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

AC-code:

此代码有点复杂。。。。。。

#include<cstdio>
#include<cstring> 
int main()
{
	int a,b,c,t,d,i,len,j,k,q,flag,p,a1,b1,c1,d1;
	char str[40],str1[10],str2[10],str3[10],str4[10];
	scanf("%d",&t);
	for(j=1;j<=t;j++)
	{
		flag=1;
		scanf("%d.%d.%d.%d",&a,&b,&c,&d);
		scanf("%s",str);
		len=strlen(str);
		i=0;
			a1=k=p=q=0;
			while(str[i]!='.'&&str[i]!='\0')
			{
				str1[a1++]=str[i++];
			}
			str1[a1]='\0';
			i++;
			while(str[i]!='.'&&str[i]!='\0')
				str2[k++]=str[i++];
			i++;
			while(str[i]!='.'&&str[i]!='\0')
				str3[p++]=str[i++];
			i++;
			while(str[i]!='.'&&str[i]!='\0')
				str4[q++]=str[i++];
			k=0;
			while(str1[k]=='0')
				k++;
			b1=c1=d1=a1=0;
			for(;k<8;k++)
			{
				if(str1[k]=='0')
					continue;
				p=1;
				for(i=0;i<7-k;i++)
				p*=2;
				a1+=p;
			}
		if(a1!=a)
			flag=0;
		else
		{
			k=0;
			while(str2[k]=='0')
				k++;
			for(;k<8;k++)
			{
				if(str2[k]=='0')
					continue;
				p=1;
				for(i=0;i<7-k;i++)
				p*=2;
				b1+=p;
			}
			if(b1!=b)
				flag=0;
			else
			{
				k=0;
				while(str3[k]=='0')
					k++;
				for(;k<8;k++)
				{
					if(str3[k]=='0')
					continue;
					p=1;
					for(i=0;i<7-k;i++)
					p*=2;
					c1+=p;
				}
				if(c1!=c)
					flag=0;
				else
				{
					k=0;
					while(str4[k]=='0')
						k++;
					for(;k<8;k++)
					{
						if(str4[k]=='0')
							continue;
						p=1;
						for(i=0;i<7-k;i++)
							p*=2;
						d1+=p;
					}
					if(d1!=d)
						flag=0;
				}
			}
		}
		if(!flag)
			printf("Case %d: No\n",j);
		else
			printf("Case %d: Yes\n",j);
	}
	return 0;
 } 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值