题解 | 合法IP
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string str;
getline(cin, str);
stringstream ss(str);
string segment;
int count = 0;
bool ip = true;
while (getline(ss, segment, '.')) {
if (segment.length() > 1 && segment[0] == '0' || segment.size() == 0 || segment[0] == '+' ) {
ip = false;
break;
}
if (segment.empty()) {
ip = false;
break;
} else {
int num = stoi(segment);
if (num < 0 || num > 255) {
ip = false;
break;
}
count++;
}
}
if (count != 4) {
ip = false;
}
if (ip) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
科大讯飞公司氛围 472人发布