【十二题解】 | #识别有效的IP地址和掩码并进行分类统计#暴力破解

识别有效的IP地址和掩码并进行分类统计

http://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682

先把所有字符读入,再判断ip和子网是否符合规则

#include<stdio.h>

int ip_judge(charip, intp){

int one[4];
int i=0;
memset(one, -1, sizeof(int)*4);
for(;ip[*p]!='~';(*p)++){
    int number_exist =0;
    int number = 0;
    for(;ip[*p] != '.' && ip[*p] != '~';(*p)++){
        int aa = *p;
        char bb = ip[*p];
        number_exist = 1;
        number=number*10+(ip[*p]-48);
    }
    if(number_exist){
        one[i]=number;
    }
    i++;
    if(ip[*p] == '~'){
        (*p)--;
    }
}
(*p)++;
int kk = *p;
int ans=0;
if(one[0]==0 || one[0]==127)return 1000;
if(one[0]==-1||one[1]==-1||one[2]==-1||one[3]==-1||one[0]>255||one[1]>255||one[2]>255||one[3]>255)return 0;
if(one[0]>=1 && one[0]<=126)ans+=1;
if(one[0]>=128 && one[0]<=191)ans+=2;
if(one[0]>=192 && one[0]<=223)ans+=3;
if(one[0]>=224 && one[0]<=239)ans+=4;
if(one[0]>=240 && one[0]<=255)ans+=5;
if(one[0]==10)ans+=10;
if(one[0]==172 && one[1]>=16 && one[1]<=31)ans+=10;
if(one[0]==192 && one[1]==168)ans+=10;
return ans;

}

int sub_judge(charip, intp){

char two[33];
memset(two, -1, sizeof(char)*33);
int i=0;
for(;ip[*p]!='\n';(*p)++)
{
    int aaaaa=*p;
    char bbbbb=ip[*p];
    int number_exist =0;
    int number = 0;
    for(;ip[*p] != '.' && ip[*p] != '\n';(*p)++){
        number_exist = 1;
        number=number*10+(ip[*p]-48);
    }
    if(number_exist){
        int times=i*8+8;
        int x=128;
        for(int first=i*8; first<times; first++){
            int bit=x & number;
            if(bit==x){
              two[first]='1';
            }
            else{
                two[first]='0';
            }
            x>>=1;
        }
    }
    i++;
    if(ip[*p] == '\n'){
        (*p)--;
    }
}
(*p)++;
int aaaa=*p;
int all_zero=1;
int all_one=1;
for(int j=0; j<32; j++){
    if(two[j]=='1')all_zero=0;
    if(two[j]=='0')all_one=0;
    if(j!=0 && j!=31 && two[j]=='1' && two[j-1]=='0' && two[j+1]=='0')return 0;
    if(j!=0 && j!=31 && two[j]=='0' && two[j+1]=='1')return 0;
    if(two[0]=='0')return 0;
    if(two[31]=='1')return 0;
    if(j==31 && two[j]=='1' && two[j-1]=='0')return 0;
}
if(all_zero || all_one)return 0;
return 1;

}

int main(){

char ip[13000];
int a=0, b=0, c=0, d=0, e=0, error=0, pri=0;
memset(ip, -1, sizeof(char)*1000);
char *loc=ip;
while(~scanf("%c", loc))loc++;
int lenth=(loc-ip)/sizeof(char);
int i;
int *p=&i;
for(i=0; i<lenth;){
    int one=ip_judge(ip, p);
    int two=sub_judge(ip, p);
    if(one && two){
        if(one!=1000){
            int x = one%10;
            one /=10;
            if(x==1)a++;
            if(x==2)b++;
            if(x==3)c++;
            if(x==4)d++;
            if(x==5)e++;
            if(one==1)pri++;
        }
    }
    else if(one!=1000){
        error++;
    }
}
printf("%d %d %d %d %d %d %d", a, b, c, d, e, error, pri);
return 0;

}

全部评论

相关推荐

Edgestr:没项目地址就干脆把那一栏删了呗
点赞 评论 收藏
分享
拒绝996的悲伤蛙:此贴终结|给路过的牛友分享一下心得👇 实习的时候不要光埋头干活,身边的大佬同事才是真·宝藏人脉!大胆请教他们工作以及职场上的问题以我的经历,我的带教有十几年工作经验,做过运维、后端开发、web测试,现在是高级软测,是行走的避坑指南 我之前纠结要不要学Web测试简历,被他一句话点醒:Web发展成熟,岗位需求在缩,AI对互联网的冲击可能以后架构+开发+测试一人包揽。现在用户更多用的是移动端APP/小程序,相比之下天天守着电脑刷网页的人基数小。 这里我的纠结得到反馈,于是我又把简历发给带教,获得了一对一的简历指导。 感兴趣的可以看看: 1.教育背景:本科→本科(全日制) 2.实习经历:总体问题不大,但第2点要稍作修改,可以写但做功课,如风机、水箱……可能会问用哪个供应商的?使用寿命、型号、电压电流、多少秒会触发逻辑? 3.项目经历(坑太多,大型翻车现场): - 项目名越直白越好,让人一眼就知道你干了啥。 -用的什么语言设计核心接口,异步执行做功课,涉及线程问题,被问可回答n个功能是如何错开异步执行的 - “验证任务消费……阻塞丢包”“高负载稳定性”这种词,没三五年开发功底不要写,不然面试时被问线程、数量级、CPU占用,内存带宽等影响性能的直接原地社死。 -做功课 -做功课,测了哪些模块,如何设计,接口流量抓包,token,变量…… -做功课,要熟悉网络协议…… 带教之前做互联网开发的时候面试过很多人,总的来说不要为了显得项目高大上过渡包装,写了就要做好拷打的准备
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务