中度题 - milkandbread/summary-interview GitHub Wiki
1 现在IPV4下用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此不需要用正号出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
现在需要你用程序来判断IP是否合法。
输入:
10.138.15.1
输出:
YES
int main()
{
string str;
while (getline(cin, str)
{
string res = "YES";
int i = 0;
while (i<str.length())
{
int tmp = 0;
if (str[i] >= '0' && str[i] <= '9')
{
tmp = str[i] - '0';
int j = i+1;
while(j < str.length() && str[j] != '.')
{
if (str[j] >= '0' && str[j] <= '9')
{
tmp = tmp * 10 + str[j]-'0';
}
else
{
res = "NO";
break;
}
j++;
}
i=j+1;
}
else
{
res = "NO";
}
if (res == "NO" || tmp > 255 || tmp < 0)
{
res = "NO";
break;
}
}
cout<< res <<endl;
}
return 0;
}