题解 | 合法IP

合法IP

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

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    stringstream ss(str);
    string segment;
    int count = 0;
    bool ip = true;

    while (getline(ss, segment, '.')) {
        if (segment.length() > 1 && segment[0] == '0' || segment.size() == 0 || segment[0] == '+' ) {
            ip = false;
            break;
        }
        if (segment.empty()) {
            ip = false;
            break;
        } else {
            int num = stoi(segment);
            if (num < 0 || num > 255) {
                ip = false;
                break;
            }
            count++;
        }
    }
    if (count != 4) {
        ip = false;
    }
    if (ip) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
nus2201602...:兄弟,你这个简历撕了丢了吧,就是一坨,去找几个项目,理解项目流程,看几遍就是你的了,看看八股就去干了,多看看牛客里别人发出来的简历,对着写,你这写的啥啊,纯一坨
点赞 评论 收藏
分享
MinJerous:虽然我一直说 计算机不怎么卡学历 但是至少得一本
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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