LightOJ 1354 - IP Checking

1354 - IP Checking




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


题目大意:IP地址的二进制形式和十进制形式是否相同?

所以我们要检索字符串的每一部分是否相同。这要靠查到对字符串的处理,我这块太差了,以前做大数问题就吃过亏,要多加练习!

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
    int t,tt=0;;
    char a[105],b[105];
    cin>>t;
    while(t--)
    {
        tt++;
        scanf("%s%s",a,b);
        int f[105]={0},g[105]={0};
        int len1=strlen(a),len2=strlen(b);
        int j=1;
        for(int i=len1-1;i>=0;i--)
        {
            int dp=1,flag=0;
            while(a[i]!='.'&&i>=0)
            {
                f[j]+=(a[i]-'0')*dp;//依次将两点间的字母转化为数字
                dp*=10;
                i--;
                flag=1;
            }
            if(flag)
                j++;
        }
        j=1;
        for(int i=len2-1;i>=0;i--)
        {
            int dp=1,flag=0;
            while(b[i]!='.'&&i>=0)
            {
                g[j]+=(b[i]-'0')*dp;//依次将两点间的字母转化为数字
                dp*=2;
                i--;
                flag=1;
            }
            if(flag)
                j++;
        }
        int out=0;//四个数字中有一个不相同就跳出
        for(int i=1;i<=4;i++)//从一开始的啊哦
        {
            if(f[i]!=g[i])
            {
                out=1;
                break;
            }
        }
        if(out)
            cout<<"Case "<<tt<<": "<<"No"<<endl;
        else
            cout<<"Case "<<tt<<": "<<"Yes"<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值