题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
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));
}
}

查看5道真题和解析