题解 | #IP地址#

IP地址

https://www.nowcoder.com/practice/2359e23180194f99828f5cd9c764236a

#include <stdio.h>
int isIplegal(int ipNum,int ipBits);
int main() {
    char ipAd[16]="";
    int ipNum,ipBits,dotCount,isLegal;
  while(scanf("%s",ipAd)!=EOF){
    ipNum=0,ipBits=0,dotCount=0,isLegal=1;
    if(ipAd[15]!='\0'||ipAd[8]=='\0')//ip长度不合法
    {
        printf("No!\n");
        continue;
    }
    for(int i=0;ipAd[i]!='\0';i++)
    {
        if(ipAd[i]<'0'||ipAd[i]>'9')
        {
            if(ipAd[i]=='.')
            {
                dotCount++;
                if(isIplegal(ipNum,ipBits)==0)
                {
                    isLegal=0;
                    break;
                }
                ipNum=ipBits=0;
            }
            else {
                isLegal=0;
                break;
            }
        }

        else if(ipAd[i]=='0')
        {
            ipBits++;
            if(ipAd[i+1]!='\0'&&ipAd[i+1]!='.'&&ipBits==1)
            {
                isLegal=0;
                break;
            }
        }
        else{
            ipBits++;
            ipNum=ipNum*10+(ipAd[i]-'0'); 
        }
    }
    if(dotCount!=3)
        isLegal=0;
    if(isIplegal(ipNum,ipBits)==0)
        isLegal=0;
    if(isLegal==0)
        printf("No!\n");
    else
        printf("Yes!\n");
  }
    return 0;
   
}

int isIplegal(int ipNum,int ipBits)
{
    if(ipNum<0||ipNum>255||ipBits<0||ipBits>3)
        return 0;
    else
        return 1;
}

全部评论

相关推荐

09-17 17:09
门头沟学院 Java
雨忄:有人给出过解法,拖晚点去,然后到时候再找其他理由商量,既增加他们的筛人成本,不一定会给你收回offer ,也能占位避免工贼
秋招的嫡长offer
点赞 评论 收藏
分享
没关系现在见到了、
真的很糟糕:见过国王的offer吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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