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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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