IP 地址

题目截图

 

 

思路:

  按格式提取 a,b,c,d,然后判断 a,b,c,d 是否合法即可。

 

代码如下:

 1 /*
 2     IP 地址 
 3 */
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <math.h>
 8 #include <stdlib.h>
 9 #include <time.h>
10 #include <stdbool.h>
11 
12 bool judge(int a) {                // 判断 a 是否合法 
13     return (a>=0 && a<=255);
14 } 
15 
16 int main() {
17     int a, b, c, d;
18     while(scanf("%d.%d.%d.%d", &a, &b, &c, &d) != EOF) {    // 输入IP地址,并提取 a,b,c,d 
19         if(judge(a) && judge(b) && judge(c) && judge(d)) {    // 判断 a,b,c,d 是否合法 
20             printf("Yes!\n");                                // 合法 
21         } else {
22             printf("No!\n");                                // 不合法 
23         }
24     } 
25 
26     return 0;
27 }

 

posted @ 2018-02-05 16:10  Just_for_Myself  阅读(174)  评论(0编辑  收藏  举报