输入一个字符串,返回其最长的数字子串,以及其长度。若有多个最长的数字子串,则将它们全部输出(按原字符串的相对位置)
本题含有多组样例输入。
数据范围:字符串长度 , 保证每组输入都至少含有一个数字
输入一个字符串。1<=len(字符串)<=200
输出字符串中最长的数字字符串和它的长度,中间用逗号间隔。如果有相同长度的串,则要一块儿输出(中间不要输出空格)。
abcd12345ed125ss123058789 a8a72a6a5yy98y65ee1r2
123058789,9 729865,2
样例一最长的数字子串为123058789,长度为9 样例二最长的数字子串有72,98,65,长度都为2
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char input[200]={0}; int max=0,flag=0,count=0,i=0,j=0; while(scanf("%s", input)!=EOF){ for(i=0; i<strlen(input); i++){ if((isdigit(input[i]) && !isdigit(input[i-1]))|| (isdigit(input[0]))){ for(j=i; isdigit(input[j])&&j<strlen(input); j++) { count++; } max = max>count?max:count; i += count; count = 0; } } for(i=0; i<strlen(input); i++){ if((isdigit(input[i]) && !isdigit(input[i-1]))|| (isdigit(input[0]))){ flag = i; for(j=i; isdigit(input[j])&&j<strlen(input); j++) { count++; } if(count == max){ for(int k=i; k<j; k++) printf("%c", input[k]); } i += count; count = 0; } } printf(",%d\n", max); max = 0; } return 0; }
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int count = 0; int i, len; char str[201]= {0}; char max_candi[201] = {0}; while (scanf("%s", str) != EOF) { len = strlen(str); char *p = str; /* 分割字符串 */ for(i = 0; i < len; i++) { if (p[i] < '0' || p[i] > '9') { p[i] = '\0'; } } /* 遍历数字字符串,记录每段的起始位置i和该段的长度 */ int max = 0; for(i = 0; i < len; i++) { if (p[i] >= '0' && p[i] <= '9') { max_candi[i] = strlen(p+i);//易错点 max = (max > max_candi[i]) ? max : max_candi[i]; i+=strlen(p+i) - 1;//易错点 } } /* 遍历输出 */ for(i = 0; i < len; i++) { if(max_candi[i] == max) { printf("%s", p+i); } } printf(",%d\n", max); memset(str,0,201*sizeof(char)); } return 0; }
#include <stdio.h> #include <string.h> int main() { char str[200]={0}, num[200]={0}; int i, j, k, count, max; while(scanf("%s", &str)!=EOF) { i=0; max=0; k=0; while(i<strlen(str)) { count=0; while(str[i]>='0' && str[i]<='9') { count++; i++; } if(count>=max) { if(count>max) k=0; //如果新的最大值比之前的最大值大,把输出子串清空 max=count>max ? count : max; for(j=0;j<max;j++) //如果新的最大值等于之前的最大值,输出子串不清空 { num[k]=str[i-count+j]; k++; } num[k]='\0'; //输出子串最后加个空字符 } i++; } printf("%s,%d\n",num,max); } return 0; }
#include <string.h> #include <stdio.h> #include <stdlib.h> #define N 100 int main() { char s[N] = {0}; while(scanf("%s", s) != EOF){ int len = strlen(s); int maxLen = 0; char res[N] = {0}; char temp[N] = {0}; int cnt = 0, tempCnt = 0; s[len++] = 'a'; for(int i = 0; i < len; i++){ if(s[i] >= '0' && s[i] <= '9'){ temp[tempCnt++] = s[i]; }else{ if(tempCnt > maxLen){ maxLen = tempCnt; cnt = 0; for(int j = 0; j < tempCnt; j++){ res[cnt++] = temp[j]; } res[cnt++] = '\0'; tempCnt = 0; }else if(tempCnt == maxLen && tempCnt > 0){ cnt--; for(int j = 0; j < tempCnt; j++){ res[cnt++] = temp[j]; } res[cnt++] = '\0'; tempCnt = 0; }else{ tempCnt = 0; } } } printf("%s,%d\n", res, maxLen); } return 0; }
一遍while找最长值,一边while打印
#include #include int main() { char str[201] = {0}, *beg, *end; int max = 0; while (scanf("%s", str) != EOF) { beg = end = str; max = 0; while (*end) { if (!isdigit(*end)) { if (max < (end - beg)) max = end - beg; beg = end + 1; } end++; } if (max < (end - beg)) max = end - beg; beg = end = str; while (*end) { if (!isdigit(*end)) { if (max == (end - beg)) { *end = 0; printf("%s", beg); } beg = end + 1; } end++; } if (max == (end - beg)) printf("%s", beg); printf(",%d\n", max); } return 0; }
#include <stdio.h> #define N 200 int main() { char str[N]; int pos[N],i=0,j,cnt,len,flag,max_len,start; while(gets(str)) { len=0;flag=0;max_len=0;cnt=0; while(str[i]!='\0') { if(str[i]>='0'&&str[i]<='9') { if(flag==0) { start=i; flag=1; } len++; } else { if(flag) { if(len>max_len) { cnt=0; max_len=len; pos[cnt++]=start; } else if(len==max_len) { pos[cnt++]=start; } flag=0; len=0; } } i++; } if(flag) { if(len>max_len) { cnt=0; max_len=len; pos[cnt++]=start; } else if(len==max_len) { pos[cnt++]=start; } } for(i=0;i<cnt;i++) { for(j=pos[i];j<pos[i]+max_len;j++) { printf("%c",str[j]); } } printf(",%d\n",max_len); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> int main() { char a[250] = { 0 }; int x[250] = { 0 }; int s, i, j,k=0,sum=0; while (gets(a) != NULL) { k = 0; int flag = 0; s = strlen(a); sum = 0; for (i = 0; i < s; i++) { if (a[i] >= '0' && a[i] <= '9') { if (flag == 0) { k = i; flag = 1; } x[k]++; } else { flag = 0; } } int max = 0; for (i = 0; i < 250; i++) { if (x[i] > max) max = x[i]; } for (i = 0; i < 250; i++) { if (x[i] == max) { for (j = i; j < i + max; j++) printf("%c", a[j]); } } printf(",%d\n", max); memset(a, 0, sizeof(a)); memset(x, 0, sizeof(x)); } return 0; }
#include <stdio.h> #include <stdlib.h> typedef unsigned char u8; u8 isnum(char c) { return (c>'9'||c<'0')?0:1; } u8 getnumlen(char* str) { u8 i; for(i=0;;i++) if(!isnum(str[i])) return i; return i-1; } #define RESET_ALL \ memset(str,0,sizeof(str));\ memset(len,0,sizeof(len));\ memset(max_index,0,sizeof(max_index));\ max = 0;\ i = 0;\ j = 0;\ k = 0; #define RESET_J \ memset(max_index,0,sizeof(max_index));\ j=0; int main() { char str[201]; u8 len[201]; u8 max_index[101]; u8 max; int i,j,k; RESET_ALL; while(EOF!=scanf("%s",str)) { for(i=0;str[i]!=0;) { if(isnum(str[i])) { len[i]=getnumlen(&str[i]); if(max<len[i]) { RESET_J; max = len[i]; max_index[j] = i; j++; } else if(max==len[i]) { max_index[j] = i; j++; } i+=len[i]; } else i++; } for(i=0;i<j;i++) for(k=0;k<max;k++) printf("%c",str[max_index[i]+k]); printf(",%d\n",max); RESET_ALL; } return 0; }
// 使用字符串数组qsort排序 #include <stdio.h> #include <string.h> #include <stdlib.h> char *g_array[100]; int g_index = 0; int GetIndex(char *s) { for (int i = 0; i < 100; i++) { if (g_array[i] != NULL && strcmp(s, g_array[i]) == 0) { return i; } } return -1; } void InsertArray(char *s) { int i = GetIndex(s); // 字符串已存在不再插入 if (i >= 0) { return; } g_array[g_index] = s; g_index++; } void FreeArray(void) { for (int i = 0; i < g_index; i++) { if (g_array[i] != NULL) { free(g_array[i]); g_array[i] = NULL; } } } int Cmp(const void *p1, const void *p2) { char *a = *(char **)p1; char *b = *(char **)p2; // 按字符串长度从小到大排序 return strlen(b) - strlen(a); } int main(void) { char str[1000] = {'\0'}; while (gets(str)) { // 多组数组需要在每次循环初始化全局变量 memset(g_array, 0, sizeof(g_array)); g_index = 0; int i = 0; while (i < strlen(str)) { if (str[i] >= '0' && str[i] <= '9') { int len = 0; int startIndex = i; while (i < strlen(str)) { // 计算数字长度 if (str[i] >= '0' && str[i] <= '9') { len++; i++; // 处理字符末尾为数字情况 if (str[i] == '\0') { char *tmp = (char *)malloc(sizeof(char) * (len + 1)); strncpy(tmp, &str[startIndex], len); tmp[len] = '\0'; InsertArray(tmp); } } else { // 复制纯数字字符串到字符串数组 char *tmp = (char *)malloc(sizeof(char) * (len + 1)); strncpy(tmp, &str[startIndex], len); tmp[len] = '\0'; InsertArray(tmp); break; } } } else { i++; } } qsort(g_array, g_index, sizeof(char *), Cmp); for (int i = 0; i < g_index; i++) { // 处理字符串长度相等情况 if (strlen(g_array[i]) == strlen(g_array[0])) { printf("%s", g_array[i]); } } printf(",%d\n", strlen(g_array[0])); FreeArray(); } }