题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
#include <bits/stdc++.h> #include <cctype> using namespace std; int main() { string s; cin >> s; bool flag = false; int temp = 0; int st = -1, ed = -1; int count = 0; for (int i = 0; i < s.size(); ++i) { if((s[i] < '0' || s[i] > ' 9') ||s[i] == '0' && (s[i + 1] != '.' && i < s.size() - 1)){ flag = false; break; } if (isdigit(s[i])) { st = i; while (isdigit(s[i])) { i++; } ed = i; temp = stoi(s.substr(st, ed - st)); if (temp > 255 || temp < 0) { flag = false; break; }else{ flag = true; count++; } } } if(flag && count == 4){ cout << "YES"; }else{ cout << "NO"; } return 0; } // 64 位输出请用 printf("%lld")