题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool numberIp(string &s)
{
string temp;
bool flag = true;
int count = 0;
for(auto &i:s)
{
if(i=='.')
{
if(temp.empty())
return false;
else
{
if(temp.size()!=1&&temp[0]=='0')
return false;
if(!isdigit(temp[0]))
return false;
int n = stoi(temp);
if(n<0||n>255)
return false;
temp.clear();
++count;
}
}
else {
temp.push_back(i);
}
}
if(count!=3)
return false;
if(temp.empty())
return false;
if(temp.size()!=1&&temp[0]=='0')
return false;
if(!isdigit(temp[0]))
return false;
int n = stoi(temp);
if(n<0||n>255)
return false;
return true;
}
int main() {
string s;
getline(cin,s);
if(numberIp(s))
cout<<"YES";
else
cout<<"NO";
}
// 64 位输出请用 printf("%lld")
傻逼题,题设啥也没有,全靠猜,然后通过应例,一个一个的改设定


查看10道真题和解析