题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
#include <iostream> #include <sstream> using namespace std; bool isnum(string& s){ int n = 0; if(s.empty()) return false; else if(s.size()>=2&&s[0]=='0') return false; for(int i=0;i<s.size();i++){ if(!isdigit(s[i])) return false; n = n*10+s[i]-'0'; } if(n>=0&&n<=255) return true; else return false; } int main() { string s; while (getline(cin, s)) { stringstream ss(s); string str; int ret = true; int cnt = 0; while(getline(ss, str, '.')){ ret&=isnum(str); cnt++; } if(cnt!=4) ret = false; cout<<(ret?"YES":"NO"); } }
猜test cases系列