lightoj 1354 - IP Checking 【进制水题】

来源:互联网 发布:修改oracle默认端口 编辑:程序博客网 时间:2024/05/16 04:39
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

 


PROBLEM SETTER: JANE ALAM JAN


题意:给定一个用十进制表示的IP和一个用二进制表示的IP,问你这两个IP是否相等。

AC代码:

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <vector>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN 50000+10#define MAXM 50000000#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;char str[50];int num[5];int main(){    int t, kcase = 1;    Ri(t);    W(t)    {        Rs(str);        int len = strlen(str);        int top = 0, sum = 0;        for(int i = 0; i < len; i++)        {            if(str[i] == '.')            {                num[++top] = sum;                sum = 0;                continue;            }            else                sum = sum * 10 + str[i] - '0';        }        num[++top] = sum;        Rs(str);        len = strlen(str);        top = 0, sum = 0;        int bit = 7;        bool flag = true;        for(int i = 0; i < len; i++)        {            if(!flag) break;            if(str[i] == '.')            {                if(sum != num[++top])                    flag = false;                sum = 0; bit = 7;                continue;            }            else            {                if(str[i] - '0' == 1)                    sum += (int)pow(2, bit);                bit--;            }        }        if(sum != num[++top])            flag = false;        if(flag)            printf("Case %d: Yes\n", kcase++);        else            printf("Case %d: No\n", kcase++);    }    return 0;}




0 0
原创粉丝点击