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

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

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

#include<stdio.h>
#include<string.h>
#define N 200
int main(){
    char str[N];
    while(scanf("%s",str)!=EOF){
        int max=0,count=0;
        for(int i=0;i<strlen(str);i++)
        {
            if(str[i]>='0'&&str[i]<='9')
                count++;
            else
                count=0;
            if(max<count)
                max=count;//找到连续数字个数的最大值
        }
        for(int i=0;i<strlen(str);)
        {
            if(str[i]>='0'&&str[i]<='9')
            {
                int j=i+1;
                while(str[j]&&str[j]>='0'&&str[j]<='9')   j++;
                if(j-i==max)    //根据这个差值进行打印输出所有满足条件的连续数字
                    while(j!=i)
                        printf("%c",str[i++]);
                else 
                    i=j;
            }
            else
                i++;
        }
        printf(",%d\n",max);
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-25 19:15
点赞 评论 收藏
分享
评论
10
收藏
分享

创作者周榜

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