IP的计算
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6237 Accepted Submission(s): 1186
Problem Description
在网络课程上,我学到了很多有关IP的知识。IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此不需要用正号出现),如192.168.100.16,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
但是粗心的我,常常将IP地址写错,现在需要你用程序来判断。
但是粗心的我,常常将IP地址写错,现在需要你用程序来判断。
Input
输入有多个case,每个case有一行,不超过100个字符。
Output
对于每个case,判断输入的IP是否正确,如果正确输入YES,否则NO。
Sample Input
192.168.100.16
Sample Output
YES/* 思路:这个题细节很多,首先得对ip有所了解; 1.必须有且只有三个"."; 2."."的两端必须是数字,并且在[0.255]; */ #include<iostream> #include<algorithm> #include<string> #include<cstdio> using namespace std; int main() { char a[102]; while(gets(a)){ int len=strlen(a); char *p; p=a; int count=0,flag=0; while(*p!='\0'){ int sum=0,tag=0,s=0; flag=0; while(*p!='\0'&&*p!='.'&&(*p>='0'&&*p<='9')){ sum=sum*10+(*p-'0'); p=p+1; tag=1; s++; } if(*p!='\0') p=p+1; if(tag){ if(*(p-1)=='.') count++; if(count>3) break; if(sum>=0&&sum<=255&&s<=3) flag=1; else break; } else break; } if(*p!='\0') cout<<"NO"<<endl; else if(flag&&count==3) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }