题解 | 合法IP
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s;
getline(cin, s);
vector<int> ve;
bool hefa = true;
string str;
for(char c : s){
if(c >= '0' && c <= '9'){
str += c;
}else if(c == '.'){
if(str.length() == 0 || (str[0] == '0' && str.length() > 1)){
hefa = false;
break;
}
ve.push_back(stoi(str));
str.clear();
}else {
hefa = false;
break;
}
}
if(!hefa){
cout << "NO" << endl;
return 0;
}
if(str.length() > 1 && str[0] == '0'){
cout << "NO" << endl;
return 0;
}
if(str.length() > 0)
ve.push_back(stoi(str));
if(ve.size() != 4){
hefa = false;
}
for(int it : ve){
if(it > 255){
hefa = false;
break;
}
}
if(hefa){
cout << "YES" << endl;
}else {
cout << "NO" << endl;
}
}
// 64 位输出请用 printf("%lld")


查看24道真题和解析