题解 | 合法IP

合法IP

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

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

int main() {
    string s;
    getline(cin, s);
    vector<int> ve;
    bool hefa = true;
    string str;
    for(char c : s){
        if(c >= '0' && c <= '9'){
            str += c;
        }else if(c == '.'){
            if(str.length() == 0 || (str[0] == '0' && str.length() > 1)){
                hefa = false;
                break;
            }
            ve.push_back(stoi(str));
            str.clear();

        }else {
            hefa = false;
            break;
        }
    }
    if(!hefa){
        cout << "NO" << endl;
        return 0;
    }
    if(str.length() > 1 && str[0] == '0'){
        cout << "NO" << endl;
        return 0;
    }

    if(str.length() > 0)
    ve.push_back(stoi(str));

    if(ve.size() != 4){
        hefa = false;
    }
    
    for(int it : ve){
        if(it > 255){
            hefa = false;
            break;
        }
    }
    if(hefa){
        cout << "YES" << endl;
    }else {
        cout << "NO" << endl;
    }
    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

脑袋锈住了:你这算啥,哥们中科院中强所硕士,本科211,叫我去干分拣,时薪20
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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