题解 | #合法IP#

合法IP

https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9

//这题不难,就是格式很令人爆炸,比如01这种不算正确,..这种也要考虑
#include<stdio.h>
#include<string.h>

int main(void)
{
    char str[100]={0};
    int flag=0;
    int flag2=0;
    while(scanf("%s",&str)!=EOF)
    {
        int len=strlen(str);
        int a=0;
        for(int i=0;i<len;i++)
        {
            int temp;
            if(str[i]!='.')
            {
                temp=(str[i]-'0');
                a=a*10+temp;
            }
            if(str[i]=='0'&&(str[i+1]>'0'&&str[i+1]<'9'))
            {
                flag--;
                if(str[i-1]>'0'&&str[i-1]<'9')
                {
                    flag++;
                }
            }
            if(str[i]=='.'||i==(len-1))
            {
                if(str[i]=='.'&&i==0)
                {
                    flag--;
                }
                if(str[i]=='.'&&i>=1&&str[i-1]=='.')
                {
                    flag--;
                }
                if(a>=0&&a<=255&&i!=0)
                {
                    flag++;
                }
                a=0;
            }
        }
        //printf("%d\n",flag);
        if(flag==4)
        {
            printf("YES");
        }
        else
        {
            printf("NO");
        }
    }
    return 0;
}

全部评论

相关推荐

把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务