O - IP Checking LightOJ - 1354

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地址,一个是十进制形式,另一个是二进制形式,问这两个IP地址是否相同

    思路;直接把二进制的数化为十进制就好了,如果四个数都相同就输出yes,否则No;(一开始用字符串写的。。很麻烦)

   下面附上代码:

   

#include<bits/stdc++.h>
using namespace std;
char s[8];
int main()
{
	int a,b,c,d,t,k=0,i,j;
	int x,y,z,l;
	cin>>t;
	while(t--)
	{
		int sum1=0,sum2=0,sum3=0,sum4=0;
		int flag1=0,flag2=0,flag3=0,flag4=0;
		scanf("%d.%d.%d.%d",&a,&b,&c,&d);
		scanf("%d.%d.%d.%d",&x,&y,&z,&l);
		i=0;
		while(x)
		{
			sum1+=pow(2,i)*(x%10);
			i++;
			x/=10;
		}
		if(sum1==a) flag1=1;
		i=0;
		while(y)
		{
			sum2+=pow(2,i)*(y%10);
			i++;
			y/=10;
		}
		if(sum2==b) flag2=1;
		i=0;
		while(z)
		{
			sum3+=pow(2,i)*(z%10);
			i++;
			z/=10;
		}
		if(sum3==c) flag3=1;
		i=0;
		while(l)
		{
			sum4+=pow(2,i)*(l%10);
			i++;
			l/=10;
		}
		if(sum4==d) flag4=1;
		printf("Case %d: ",++k);
		if(flag1&&flag2&&flag3&&flag4) puts("Yes");
		else puts("No");
	}
	
	return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值