链接:
https://vjudge.net/problem/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.
思路:
直接进制转换
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 5e4+10;
const int MOD = 1e9+7;
bool Ok(int a, char *s)
{
int sum = 0;
for (int i = 0;i < 8;i++)
{
if (s[i] == '0')
continue;
sum += 1<<(7-i);
}
return a == sum;
}
int main()
{
int t, cnt = 0;
scanf("%d", &t);
while(t--)
{
char s[100];
int a, b, c, d;
printf("Case %d:", ++cnt);
scanf("%d.%d.%d.%d", &a, &b, &c, &d);
scanf("%s", s);
bool ok = true;
if (!Ok(a, s))
ok = false;
if (!Ok(b, s+9))
ok = false;
if (!Ok(c, s+18))
ok = false;
if (!Ok(d, s+27))
ok = false;
if (ok)
puts(" Yes");
else
puts(" No");
}
return 0;
}