题目
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 the 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
一道简单的模拟题,注意输出格式Case:
AC代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int n,cnt;
int main()
{
cin >> n;
while(n--)
{
string a,b;
cin >> a >> b;
int num = 0,cur = 1;
vector<int> array1,array2;
for(int i = a.length()-1; i >= 0; i--)
{
if(i == 0)
{
num += (a[i] - '0')*cur;
cur *= 10;
array1.push_back(num);
break;
}
if(a[i] != '.')
{
num += (a[i] - '0')*cur;
cur *= 10;
}
else
{
array1.push_back(num);
num = 0;
cur = 1;
}
}
num = 0,cur = 1;
for(int i = b.length()-1; i >= 0; i--)
{
if(i == 0)
{
if(b[i] == '1')
{
num += cur;
}
array2.push_back(num);
break;
}
else
{
if(b[i] == '0')
{
cur *= 2;
continue;
}
else if(b[i] == '1')
{
num += cur;
cur *= 2;
}
else
{
array2.push_back(num);
num = 0;
cur = 1;
}
}
}
int sign = 1;
while(!array1.empty())
{
if(array1.back() == array2.back())
{
array1.pop_back();
array2.pop_back();
continue;
}
else
{
array1.clear();
array2.clear();
sign = 0;
break;
}
}
if(sign)
{
cout << "Case " << ++cnt << ": Yes" << endl;
}
else
{
cout << "Case " << ++cnt << ": No" << endl;
}
}
return 0;
}