题解 | #合法IP#

合法IP

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

虽然但是,#include+stringstream真的很好用。。自动匹配字符串整型这些。。但下面这个方法是错的,通不过01.2.3.8

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
    string ip;
    getline(cin,ip);
    stringstream s;
    char a,b,c;
    unsigned a1,b1,c1,d1;
    bool res;
    s<<ip;
    s>>a1>>a>>b1>>b>>c1>>c>>d1;
    if(a1 >= 0 && a1 <= 255 && b1 >= 0 && b1 <= 255 && c1 >= 0 && c1 <= 255 && d1 >= 0 && d1 <= 255)
    {
       cout<<"YES"<<endl; 
    }
    else cout<<"NO"<<endl;
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务