题解 | #合法IP#

合法IP

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

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

int main() {
    string strs;
    cin >> strs;              //得到输入的数据
    stringstream input(strs); //将数据变成流的形式
    vector<int> NUM;          //保存数据的容器 
    string data;              //临时保存数据的地方
    while(getline(input,data,'.'))
    {
        if (data == "\0")
        {
            break;
        }
        else if (data.size() > 1 && data[0] == '0') 
        {
            break; // 数字有前导0
        }
        for (char c : data)
        {
            if (!isdigit(c))
            {
                data = "10000";   //如果输入的数据包含字符,则把data赋值一个不属于ip地址的数
                break;
            }
        }
        NUM.push_back(stoi(data));
    }

    if (NUM.size() != 4)
    {
        cout << "NO";
    }
    else 
    {
        int cnt = 0;
        for (auto it : NUM)
        {
            if (it <= 255 && it >= 0)
            {
                cnt += 1;
            }
        }
        if (cnt == 4)
        {
            cout << "YES";
        }
        else 
        {
            cout << "NO";
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

LuvSran:是人我吃。老师就是学校呆久了,就业方面啥都不懂,还自以为是为了我们就业好。我学校就一破双非,计科入行率10%都没有,某老师还天天点名,说是出勤率抬头率前排率高了,华为什么的大厂就会来,我们就是不好好上课才没有厂来招。太搞笑了
点赞 评论 收藏
分享
迷茫的大四🐶:💐孝子启动失败,改为启动咏鹅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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