本题就是统计数字,并且实时更新所在位置和数num,最后别忘了重置num=0,否则if(num>=num_max会一直成立,得不到最后结果,用num——max和你本轮得到的num比较,看是否需要更换位置)

#include<stdio.h> #include<string.h> int main() { char st[300]; gets(st); int len = strlen(st); int p = 0, num = 0, num_max = 0; int p_max; int flag = 0; for (int i = 0; i < len; i++) { if (st[i] >= '0' && st[i] <= '9') { p = i, flag = 1;//记录此时p的位置,flag表示此时找到了数字,flag=0时是字母 } while (flag) { if (p <= len - 2 && st[p + 1] >= '0' && st[p + 1] <= '9') { num++;//统计后面连续的数字个数 p++;//更新p的位置到下一个元素进行判断是否为数字 } else break;//当不是数字,就跳出flag循环 } if (num >= num_max) { num_max = num;//更新位置 p_max = i; } num = 0;//重置num } for (int j = p_max; j <= p_max + num_max; j++) { //一定要用pmax,只用p是不行的 printf("%c", st[j]);//有了位置和长度,就输出打印 } return 0; }