温姑娘


私信TA

用户名:151210107

访问量:6736

签 名:

等  级
排  名 6474
经  验 1352
参赛次数 1
文章发表 6
年  龄 0
在职情况 学生
学  校 平顶山学院
专  业

  自我简介:

解题思路:

这个题目我采用逆向思维,直接把字符串中的数字求出来,然后用.连接。看它和原来的串相不相等。

相等后,判断数字个数必须为4个,且数字不能越域。

注意事项:


参考代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <string>
using namespace std;

char str[110],len;
int a[110],cnt;

bool judge() {
    string s="",ip="";
    int num = 0,cnt = 0;
    for(int i = 0; i < len; i++) {
        ip += str[i];
        if(str[i]>='0' && str[i]<='9') {
            num = num*10 + str[i]-'0';
        }
        else {
           a[cnt++] = num;
           num = 0;
        }
    }
    a[cnt] = num;
    cnt = min(cnt,3);
    for(int i = 0; i <= cnt; i++) {
        if(a[i] == 0) {
            s += '0';
        }
        else {
            string temp = "";
            while(a[i]) {
                char ch = a[i]%10 + '0';
                temp += ch;
                a[i] = a[i]/10;
            }
            for(int i = temp.length()-1; i >= 0; i--) {
                s += temp[i];
            }
        }
        s += '.';
    }
    ip += '\0';
    int sLen = s.length();
    s[sLen-1] = '\0';
    //cout<<s<<endl;
    if(s == ip) {
        num = 0;
        for(int i = 0; i < (int)s.length(); i++) {
            if(s[i]>='0' && s[i]<='9') {
                num = num*10 + s[i]-'0';
            }
            else {
                if(num>=0 && num<=255) {
                    num = 0;
                }
                else {
                    return false;
                }
            }
        }
        if(num >= 0 && num <= 255) {}
        else return false;
    }
    else {
        return false;
    }
    if(cnt < 3) return false;
    return true;
}
int main() {
    while(gets(str)) {
        len = strlen(str);
        bool flag = judge();
        if(flag) {
            printf("Y\n");
        }
        else {
            printf("N\n");
        }
    }
    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区