解题思路:
读入一行,按“.”分开,接下来如果是字母,输出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 人评分
C二级辅导-阶乘数列 (C语言代码)浏览:618 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:716 |
永远的丰碑 (C语言代码)浏览:656 |
字符串输入输出函数 (Java代码)浏览:1441 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:589 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)from DQM浏览:673 |
1113题解浏览:791 |
A+B for Input-Output Practice (III) (C语言代码)浏览:569 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:514 |
字符串比较 (C语言代码)浏览:685 |