[恢]hdu 2206
2011-12-25 11:39:37
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2206
题意:判断一个字符串是否是合法IP。
mark:没啥好说的,写就好了。
代码:
# include <stdio.h>
char str[110] ;
int test (char str[])
{
int i, flag = 0, buff, cnt = 0 ;
for (i = 0 ; str[i] ; i++)
{
if (str[i] != '.' &&
(str[i] > '9' || str[i] < '0'))
return 0 ;
}
for (i = 0, buff = 0 ; str[i] ; i++)
{
// printf ("%d\n", i) ;
if (flag == 0)
{
if (str[i] == '.') return 0 ;
buff = str[i] - '0' ;
flag = 1 ;
continue ;
}
if (flag == 1)
{
if (str[i] == '.')
{
if (buff > 255) return 0 ;
buff = 0 ;
flag = 0 ;
cnt ++ ;
continue ;
}
else
{
buff = buff * 10 + (str[i]-'0') ;
if (buff > 255) return 0 ;
}
}
}
if (buff > 255) return 0 ;
if (cnt != 3) return 0 ;
return 1 ;
}
int main ()
{
while (gets(str))
puts (test(str) ? "YES" : "NO") ;
return 0 ;
}