HDU2206 IP的计算 简洁AC

  给定一个IP要求判断是否为正确IP。这里用sscanf进行处理较为方便。在数据末尾添加一个tail字符来判定是否还有缀余字符,利用到该函数的返回值。

  

#include <cstdio>
#include
<cstring>
#include
<cstdlib>
using namespace std;

bool r( int x )
{
return x>= 0&& x<= 255? 1: 0;
}

int main()
{
char ip[105];
while( gets( ip ) )
{
int a, b, c, d, len= strlen( ip ), flag= 0;
if( strchr( ip, '+' )|| strlen( ip )> 15 )
{
puts(
"NO" );
continue;
}
char tail;
if( sscanf( ip, "%d.%d.%d.%d%c", &a, &b, &c, &d, &tail ) == 4 )
{
if( r(a)&& r(b)&& r(c)&& r(d) )
puts(
"YES" );
else
puts(
"NO" );
}
else
puts(
"NO" );
}
return 0;
}

  

  

  

原文地址:https://www.cnblogs.com/Lyush/p/2141609.html