题解 | #合法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”这样的存在

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务