加油吧少年


私信TA

用户名:1187276773

访问量:3943

签 名:

一起刷题,qq:1187276773

等  级
排  名 16235
经  验 769
参赛次数 0
文章发表 6
年  龄 21
在职情况 学生
学  校 石家庄铁道大学
专  业 计算机科学与技术

  自我简介:

解题思路:

读入一行,按“.”分开,接下来如果是字母,输出N;如果是数字,进行判断。



注意事项:
string流的应用,string与int的转换,统计思想。




参考代码:

#include<iostream>
#include<string>
#include<set>
#include<vector>
#include<iterator>
#include<algorithm>
#include<sstream>
using namespace std;
int main()
{
 string s;
 string temp="";
 int ip;int count=0;
 char c;
 while(cin>>s)
 {
    count=0; temp="";
     for(int i=0;i<s.length();i++)  //判断该行ip
     {
        c=s[i];
        if(c>='a'&&c<='z') {
        cout<<"N"<<endl;break;
        }
       if(c=='.')   //判断"."之前
       {
            stringstream stream(temp);  
         stream>>ip;  
             {
           if(ip>=0&&ip<=255&&count!=3){
           }
           else {   //一次不符合就判断下一个
                 cout<<"N"<<endl;break;
                }
           }
           count++;    //.个数统计
           temp="";
           }
        else    temp+=c;
        
        if(i==s.length()-1&&count==3)
        {
         stringstream stream(temp);  
         stream>>ip;  
           if(ip>=0&&ip<=255)
           {
            cout<<"Y"<<endl;
           }
       }
     }
 }
 return 0;
}

 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区