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
Inputcopy | Outputcopy |
---|---|
2 192.168.0.100 11000000.10101000.00000000.11001000 65.254.63.122 01000001.11111110.00111111.01111010 | Case 1: No Case 2: Yes |
模拟即可
#include<bits/stdc++.h>
using namespace std;
using i64 = int64_t;
#define int i64
#define endl '\n'
int n,m,sum,ans,A,B;
/*
*/
void solve()
{
int t;
cin >> t;
for(int i = 1;i <= t;i++)
{
bool is = true;
string str;
char ch;
string ans1 = "";
vector<int>v;
vector<string>s;
cin >> str;
std::stringstream s2(str);
while (getline(s2, str, '.'))
v.push_back(stoi(str));
cin >> str;
std::stringstream ss(str);
while (getline(ss, str, '.'))
s.push_back(str);
for(int i = 0;i < 4;i++)
{
string s3 = bitset<32>(v[i]).to_string();
s3 = s3.substr(24,8);
if(s[i] != s3)
{is = false;break;}
}
is ? ans1 = "Yes" : ans1 = "No";
cout << "Case "<< i << ": " << ans1 << endl;
}
}
signed main()
{
// cin.tie(0) -> sync_with_stdio(false);
int T = 1;
//cin >> T;
while(T--)
solve();
return 0;
}