题解 | #合法IP#

合法IP

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

没有优化,但是AC100%题解有些通不过的,要注意每个是否有数字,或者像01这种不合法的

#include <algorithm>
#include <sstream>
#include <vector>

using namespace std;

int main() {
    string str;
    while (getline(cin, str)) {
        istringstream ss;
        ss.str(str);
        string temp;
        vector<string> strv;
        while (getline(ss, temp, '.')) strv.push_back(temp);
        int i = 0;
        for (i = 0; i < strv.size(); i++) {
            if(strv.size()>4)break;
            if (strv[i].empty())break;
            bool digit =false;
            for(int j=0;j<strv[i].size();j++){
                if(!isdigit(strv[i][j])) {digit =false;break;}
                digit=true;
            }
            if(!digit)break;
            if(strv[i].size()>1&&strv[i][0]=='0')break;
            int x = stoi(strv[i]);
            if (x >= 0 && x <= 255) continue;
            else break;
        }
        if (i < 4) cout << "NO" << endl;
        else cout << "YES" << endl;

    }
}


全部评论

相关推荐

06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:10
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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