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

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

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

#include <ctype.h>
#include <stdio.h>
#include <string.h>
typedef struct{
    int head_index;
    int count;
}a ;

int main() {
    char str[200];
    a cc[20];
    memset(str,0,sizeof(str));
    while(~scanf("%s",str))
    {
        int n = strlen(str);
        int i;
        int k = 0;
        int flag = 0;
        int max = 0;
        int count = 1;
        for(i=0;i<=n;i++)
        {
            if(isdigit(str[i]) && !flag)
            {
                flag = 1;
                cc[k].head_index = i;
            }
            else if(!isdigit(str[i]) && flag) 
            {
                flag = 0;
                max = max>count?max:count;
                cc[k].count =count;
                count = 1;
                k++;
            }
            else if(flag)
            {
                count++;
                
            }

        }

        for(i = 0;i<k;i++)
        {
            if(cc[i].count == max)
            {
                for(int j = cc[i].head_index; j < cc[i].head_index + max; j++)
                {
                    printf("%c",str[j]);
                }
                
            }
            
        }
        printf(",%d\n",max);




        memset(str,0,sizeof(str));
    }
}

全部评论

相关推荐

牛客73617529...:无端端被你骂一句
点赞 评论 收藏
分享
07-09 12:12
门头沟学院 Java
5月底投简历7月初开奖收获秋招第一个offer,虽然白菜价,但至少能保底了
土木转行ing:土木博士想转图像,最后拿了 tp 提前批 sp 最低档,感觉性价比不高
TP-LINK开奖132人在聊
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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