题解 | #合法IP#

合法IP

https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9

#include <iostream>
#include <sstream>
using namespace std;
#include <string>
#include<istream>
int main() {
    string str;
    getline(cin, str);
    int num;
    string str2;
    bool flag = true;
    istringstream is(str);
    int cnt = 0;
    while (getline(is, str2, '.')) {
        if (str2 != "") {
            num = stoi(str2);
            if (num < 0 || num > 255 || str2[0] == '+' ||(str2[0] == '0' && str2.size()!=1)) {
                flag = false;
            }
        }
        else{
            flag = false;
        }


        cnt++;
    }
    if (cnt != 4) {
        flag = false;
    }
    if (flag) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

}

纯纯的坑题,要注意‘+’和“01”“.1.1.1”这样的存在

全部评论

相关推荐

没有offer的呆呆:薪资有的时候也能说明一些问题,太少了活不活得下去是一方面,感觉学习也有限
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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