题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
这题不难,但是是我提交次数最多的一道题,就是不标准规则没说清,比如01不符合,转化为整数默认为1,踩这种陷阱太多了 #include <iostream> #include <sstream> #include <string> #include <deque> using namespace std; int main(){ string str, temp = ""; bool flag = true; int part = 0; cin >> str; for(int i = 0; i <= str.size(); i++) { if (str[i] == '.' || i == str.size()) { if (temp.empty()) { flag = false; break; } else if (temp[0] == '0' && temp.size() > 1) { flag = false; } int num = stoi(temp); if (num > 255 || num < 0) { flag = false; } else { temp = ""; } part++; } else if (str[i] >= '0' && str[i] <= '9'){ temp += str[i]; } else { flag = false; break; } } if (flag && part == 4) cout << "YES" << endl; else cout << "NO" << endl; }