题解 | #合法IP#

合法IP

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

#include <cctype>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

bool check_(string s){
    for (auto ch : s){
        if (!isdigit(ch))
            return false;
    }
    int num_index = 0;
    if (s.size() > 1){
        for (auto ch : s){
            if (ch == '0' && num_index == 0)
                return false;
            if (ch != '0')
                num_index = 1;
        }
    }
    int x = stoi(s);
    if (x < 0 || x > 255)
        return false;
    return true;
}
int main() {
    string str;
    while (cin>>str){
        vector<string> vec;
        string tmp;
        for (int i = 0; i < str.size(); i++){
            if (i == str.size() - 1 ){
                tmp += str[i];
                vec.push_back(tmp);
            }
            if (str[i] == '.' && !tmp.empty()){
                vec.push_back(tmp);
                tmp = "";
            }
            else {
                tmp += str[i];
            }
        }
        int index = 0;
        if (vec.size() != 4){
            index = 1;
        }         
        for (auto it : vec){
            if (!check_(it)){
                index = 1;
            }
        }
        if (index)
            cout<<"NO";
        else
            cout<<"YES";
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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