您的代码已保存
答案错误:您提交的程序没有通过所有的测试用例点击对比用例标准输出与你的输出
case通过率为90.00%
用例:
255.0.0.0
193.194.202.15
232.43.7.59
对应输出应该为:
1
你的输出为:
2
#include <bits/stdc++.h>
using namespace std;
struct IP {
int a, b, c, d;
IP() {}
IP(int a, int b, int c, int d) {
this->a = a;
this->b = b;
this->c = c;
this->d = d;
}
bool operator==(const IP& ip) {
return (a == ip.a &&
b == ip.b &&
c == ip.c &&
d == ip.d);
}
IP operator&(const IP& ip) {
IP ans;
ans.a = a & ip.a;
ans.b = b & ip.b;
ans.c = c & ip.c;
ans.d = d & ip.d;
return ans;
}
bool isValid() {
return a >= 0 && a <= 255 &&
b >= 0 && b <= 255 &&
c >= 0 && c <= 255 &&
d >= 0 && d <= 255;
}
};
bool isIpStr(const string &s) {
int dotNum = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '.' || (s[i] >= '0' && s[i] <= '9'));
else return false;
if (s[i] == '.') dotNum++;
}
if (dotNum != 3) return false;
for (int i = 0; i < s.size() - 1; i++)
if (s[i] == '.' && s[i] == s[i + 1]) return false;
return true;
}
IP getIP(string& s) {
IP ans;
string t;
stringstream ss(s);
getline(ss, t, '.'); ans.a = atoi(t.c_str());
getline(ss, t, '.'); ans.b = atoi(t.c_str());
getline(ss, t, '.'); ans.c = atoi(t.c_str());
getline(ss, t, '.'); ans.d = atoi(t.c_str());
return ans;
}
int main(void) {
//ifstream cin("../in.txt");
string str, str1, str2, str3;
while (cin >> str1 >> str2 >> str3) {
if (!isIpStr(str1) || !isIpStr(str2) || !isIpStr(str3)) {
cout << 1 << endl;
continue;
}
IP mask, ip1, ip2;
mask = getIP(str1);
ip1 = getIP(str2);
ip2 = getIP(str3);
if (!mask.isValid() || !ip1.isValid() || !ip2.isValid()) {
cout << 1 << endl;
continue;
}
IP ans1 = mask & ip1;
IP ans2 = mask & ip2;
if (ans1 == ans2) cout << 0 << endl;
else cout << 2 << endl;
}
}
京公网安备 11010502036488号