题解 | #IP地址#

IP地址

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

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

bool CheckIp(string ip){
    if(ip.length()>15||ip.length()<7)
        return false;
    int temp=-1;
    int count=0;
    for(int i=0;i<ip.length();i++){
        if(ip[i]!='.'&&(ip[i]>'9'||ip[i]<'0'))
            return false;
        if(ip[i]!='.'){
            if(temp==-1)
                temp=0;
            temp=temp*10+int(ip[i]-'0');
        }
        else{
            if(temp<0||temp>255)
                return false;
            temp=-1;
            count++;
        }
        
    }
    if((temp<0||temp>255)||count!=3)
       return false;
     return true;
    


}
int main() {
    vector<string> ips;
    string input;
    while(getline(cin,input))
    {
        ips.push_back(input);
    }
    for(int i=0;i<ips.size();i++)
    {
        if(CheckIp(ips[i]))
            cout<<"Yes!"<<endl;
        else 
            cout<<"No!"<<endl;

    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-22 21:10
投递恒生电子股份有限公司等公司6个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务