题解 | #IP地址#

IP地址

https://www.nowcoder.com/practice/2359e23180194f99828f5cd9c764236a

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

int main() {
    string str;
    while(cin>>str){
        int i=0,len=0,flag=0;       //i和len标识当前整数
        for(int j=0;j<str.size();++j){
            if(str[j]=='.'){        //判断+更新
                int cur=stoi(str.substr(i,len));
                if(255>=cur && cur>=0){
                    i=j+1;
                    len=0;
                }
                else{
                    flag=1;
                    break;
                }
            }
            else len++;
        }
        if (flag){      //不符合
            cout<<"No!"<<endl;
            continue;
        }
        int cur=stoi(str.substr(i,len));    //判断最后一个整数
                if(255>=cur && cur>=0){
                    cout<<"Yes!"<<endl;
                }
                else cout<<"No!"<<endl;
    }
}

全部评论

相关推荐

11-07 16:07
复旦大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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