题解 | 在字符串中找出连续最长的数字串
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int _cmp(const char** a,const char ** b)
{
return (strlen(*b)-strlen(*a));
}
int main() {
char s[200]={0};
gets(s);
char *ps=s;
char* _num[100]={0};
int cnt=0;
for(int i=0; i<100; i++)//创建数组存储所有数字子串
{
_num[i]=(char*)malloc(30);
}
while(*ps!='\0')//将所有数字子串存在_num中
{
if(*ps>='0'&&*ps<='9')
{
int i=0;
while(*ps>='0'&&*ps<='9')
{
*(_num[cnt]+i)=*ps;
ps++;
i++;
}
cnt++;
}
ps++;
}
qsort(_num,cnt,sizeof(_num[0]) , _cmp);//对多个数字子串按长度排序
int max=strlen(_num[0]);//最大长度
for(int j=0; strlen(_num[j])==max; j++)
{
printf("%s",_num[j]);
}
printf(",%d",max);
return 0;
}
查看9道真题和解析