字符串中找出连续最长的数字串
时间限制:C/C++ 1秒,其他语言 2秒
空间限制:C/C++ 32768K,其他语言 65536K
64bit IO Format: %lld
本题可使用本地IDE编码,不做跳出限制,编码后请点击“保存并调试”按钮进行代码提交。
题目描述
读入一个字符串str,输出字符串str中的连续最长的数字串
输入描述:
个测试输入包含1个测试用例,一个字符串str,长度不超过255。
输出描述:
在一行内输出str中里连续最长的数字串。
示例1
输入
abcd12345ed125ss123456789
输出
123456789
实现
测试通过
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable : 4996)
int main(){
char *str;
str = (char*)malloc(256);
scanf("%s", str);
int len = 0;
len = strlen(str);
int index_str, len_str=0;
int max_index_str, max_len_str=0;
for (size_t i = 0; i < len; ++i){
if (*(str + i) >= '0'&&*(str + i) <= '9'){
if (i == 0){
index_str = 0;
len_str = 1;
}
else{
if ((*(str + i - 1))<'0' || (*(str + i - 1))>'9'){
if (max_len_str < len_str){
max_index_str = index_str;
max_len_str = len_str;
}
index_str = i;
len_str = 1;
}
else{
len_str++;
}
}
}
}
if (max_len_str < len_str){
max_index_str = index_str;
max_len_str = len_str;
}
for (size_t i = 0; i < max_len_str; i++){
printf("%c", *(str +max_index_str+ i));
}
printf("\n");
free(str);
return 0;
}