题解 | #合法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系列

全部评论

相关推荐

09-30 15:27
已编辑
成都工业学院 企业文化
Morpheus_:候选人:还需要测验武力值?
投递腾讯等公司10个岗位
点赞 评论 收藏
分享
未知的命运:大佬这都找不到我还找啥啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务