题解 | #在字符串中找出连续最长的数字串#

在字符串中找出连续最长的数字串

https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec

#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main() {
    char input[200];
    while (scanf("%s", input) != EOF) { 
        int startindex[100];
        int numofstart = 0;
        int maxlen = 0;
        int len = strlen(input);
        for(int i = 0; i < len; i++) {
            if(isdigit(input[i])) {
                int tempmax = 0;
                int tempstart = i;
                while(i < len && isdigit(input[i])) {
                    tempmax++;
                    // printf("isdigit %d ,tempmax %d\n", input[i] - '0', tempmax);
                    
                    i++;
                }
                if(tempmax > 0 && tempmax == maxlen) {
                    startindex[numofstart++] = tempstart;
                } else if(tempmax > maxlen) {
                    maxlen = tempmax;
                    numofstart = 0;
                    startindex[numofstart++] = tempstart;
                }
            }
        }


        for(int i = 0; i < numofstart; i++) {
            for(int j = 0; j < maxlen; j++) {
                printf("%c", input[startindex[i]+j]);
            }
        }
        printf(",%d\n", maxlen);
    }
    return 0;
}

全部评论

相关推荐

机械打工仔:第一位颇有孟德之志
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-25 19:15
点赞 评论 收藏
分享
买蜜雪也用卷:我觉得应该没有哪个人敢说自己熟练使用git,代码分支一复杂还是得慢慢寻思一下的,不过基本的拉代码提交代码还有分支什么的是应该会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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