题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
C语言解法 #include <stdio.h> #include <string.h> #include <stdlib.h> #define SIZE 205 typedef struct{ char *pa; int num; }Number; int main() { char str[SIZE]={'\0'}; while(fgets(str,SIZE,stdin)!=NULL){ char *p=strchr(str,'\n'); *p='\0'; Number trace[200]; int i,j,k=1,h=0,len=strlen(str),maxlen=0; for(i=0;i<len;){ if(str[i]>='0'&&str[i]<='9'){ trace[h].pa=&str[i]; for(j=i;j<len;j++){ if(str[j+1]>='0'&&str[j+1]<='9'){ k++; } else{ maxlen=maxlen>k?maxlen:k; i+=k; break; } } trace[h++].num=k; k=1; } else{ i++; } } for(i=0;i<h;i++){ if(trace[i].num==maxlen){ for(j=0;j<maxlen;j++){ printf("%c",*(trace[i].pa+j)); } } } printf(",%d\n",maxlen); } }