题解 | #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;
}

全部评论

相关推荐

2025-12-18 19:36
已编辑
门头沟学院 Java
程序员牛肉:可以的,简历没毛病了。 虽然还是偏向同质化,不过学历不错。后续我觉得重心放到刷实习+摆脱同质化问题上
实习简历求拷打
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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