该题我的想法是把每一个ip看出一个整数,将读取得到的数据一一与给定的ip范围比较即可。另外本题应该注意的地方是scanf读取俩字符串的方法。

代码如下:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<iostream>
  4 using namespace std;
  5 
  6 unsigned long long range[8][2] = {
  7     {10000000000,10255255255},
  8     {172016000000,17231255255},
  9     {192168000000,192168255255},
 10 
 11     {1000000000,126255255255},
 12     {128000000000,191255255255},
 13     {192000000000,223255255255},
 14     {224000000000,239255255255},
 15     {240000000000,255255255255},
 16 };
 17 
 18 int explain(char* ip){
 19      int expect = 1;
 20      int cnt = 0;
 21      for(int i = 0 ; i < strlen(ip) ; i ++){
 22 
 23         if(ip[i] >= '0' && ip[i] <= '9'){
 24             expect = 1;
 25         }
 26         else{
 27             if(expect == 0) return -1;
 28             expect = 0;//下一个字符必须是数字
 29             cnt ++;
 30         }
 31      }
 32     if(cnt != 3) return -1;// 必须有三个.符号
 33     return  1;
 34 }
 35 
 36 int main(){
 37     char ip[20] , code[20];
 38     int a = 0 , b = 0 , c = 0 , d = 0 , e = 0 ,eip = 0 , sip = 0;
 39     int cnt = 0;
 40     while(scanf("%[^~]~%s",ip,code) != EOF){
 41         int cds = explain(code);
 42         int flag = 0;
 43         if(cds > 0){
 44              int aa[4];
 45              sscanf(code , "%d.%d.%d.%d" , &aa[0],&aa[1],&aa[2],&aa[3]);
 46             if(aa[0] > 255 || aa[1] > 255 || aa[2] > 255 || aa[3] > 255 || (aa[0] == 255 && aa[1] == 255 && aa[2] ==255 &&aa[3]==255 )){
 47             //qudiao 255.255.255.255 以及在某个位上大于255的不合法的mask
 48                 eip ++;
 49                 flag = 1;
 50             }
 51             int exp = 1;
 52             for(int i = 0 ; !flag && i < 4 ; i ++){
 53                 if(exp == 0 && aa[i] || aa[i] > 255){
 54                     eip ++;
 55                     flag = 1;
 56                     break;
 57                 }
 58                 int a1 = aa[i];
 59                 if(a1 < 255){
 60                     if(a1 == 128 || a1 == 192 || a1 == 224 || a1 == 240 || a1 == 248 || a1 == 252 || a1 == 254){
 61                         exp = 0;
 62                     }else if(a1 == 0 && i > 0){
 63                         exp = 0;//去除0.0.0.0
 64                     }
 65                     else{
 66                         eip ++;
 67                         flag = 1;
 68                         break;
 69                     }
 70                 }
 71            }
 72         }else{
 73             flag = 1;
 74             eip ++;
 75         }//end
 76         if(flag == 0){
 77             int ips = explain(ip);
 78             if(ips > 0){
 79 
 80                 int a1 , b1 ,c1 ,d1;
 81                 sscanf(ip , "%d.%d.%d.%d" , &a1,&b1,&c1,&d1);
 82                 if(a1>255 || b1 > 255 || c1 > 255 || d1 > 255){
 83                     eip++;
 84                     flag = 1;
 85                 }
 86                 if(flag == 0){
 87                     long long ans = a1*1e9 + b1*1e6 + c1*1e3 + d1;
 88                     //cout<<ans<<endl;
 89                     int i = 0;
 90                     for( ; i < 8 ; i ++){
 91                         if(ans <= range[i][1] && ans >= range[i][0]){
 92                             // cout<<i<<endl;
 93                             if(i == 3) a ++;
 94                             if(i == 4) b ++;
 95                             if(i == 5) c ++;
 96                             if(i == 6) d ++;
 97                             if(i == 7) e ++;
 98                             if(i == 0 || i == 1 || i == 2) sip ++;
 99                             //break;
100                         }
101                     }
102                 }
103             }else{
104                 eip ++;
105             }
106         }
107        memset(ip,'\0',sizeof ip);
108        memset(code,'\0',sizeof(code));
109        getchar();
110     }
111     printf("%d %d %d %d %d %d %d\n",a,b,c,d,e,eip,sip);
112     return 0;
113 }

 

posted on 2016-09-03 09:52  fqbrighter  阅读(1252)  评论(0编辑  收藏  举报