Light oj1354:IP Checking

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

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;
 } 


你可能感兴趣的:(Light oj1354:IP Checking)