题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
#include <stdio.h> #include <stdlib.h> #include <string.h> int num(char n){ if(n>='0'&&n<='9') return 1; else return 0; } int main() { char a1[19]={0}, b1[19]={0},c1[19]={0},d1[19]={0},e1[19]={0}; scanf("%[^.].%[^.].%[^.].%[^\nor^.].%[^\n]", a1,b1,c1,d1,e1); int a2=strlen(a1),b2=strlen(b1),c2=strlen(c1),d2=strlen(d1),e2=strlen(e1); if(a2==0||b2==0||c2==0||d2==0||e2!=0) printf("NO"); else {int a,b,c,d; a=atoi(a1); b=atoi(b1); c=atoi(c1); d=atoi(d1); if(a>255||a<0||b>255||b<0||c>255||c<0||d>255||d<0) printf("NO"); else if((a1[0]=='0'&&num(a1[1]))||(b1[0]=='0'&&num(b1[1]))||(c1[0]=='0'&&num(c1[1]))||(d1[0]=='0'&&num(d1[1]))) printf("NO"); else if(num(a1[0])==0||num(b1[0])==0||num(c1[0])==0||num(d1[0])==0) printf("NO"); else printf("YES");} return 0; }