如何输出一个字符串中的连续最大递增数字子串(C实现)

如:
输入
f123fffwf3210abcd
输出为
123
输入:f1234hg3210ghlgh345
输出:1234

#include <stdlib.h>
#include <string.h>
#include "oj.h"

/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
*/
unsigned int Continumax(char** pOutputstr, char* intputstr)
{
char *temp_input=intputstr;
char **temp_output=pOutputstr;
int flag=0,cur_count=0,max_count=0,max_pos=0,cur_pos=0;
char pre_str;
if(!intputstr||!(*pOutputstr))
return 0;
while(*temp_input!='\0')
{
if(*temp_input>'9'||*temp_input<'0')
{
flag=0;
if(max_count<cur_count)
{
max_pos=cur_pos-cur_count;
max_count=cur_count;
}
cur_count=0;
}
else
{
if(flag==0)
{
cur_count=1;
pre_str=*temp_input;
}
else if(0==pre_str-*temp_input)
{
cur_count=1;
}
else if(flag==1&&pre_str-*temp_input==1)
{
cur_count++;
pre_str=*temp_input;
}
else if(flag==-1&&*temp_input-pre_str==1)
{
cur_count++;
pre_str=*temp_input;
}
else
{
cur_count=1;
pre_str=*temp_input;
}
}
cur_pos++;
temp_input++;
}
if(max_count<cur_count)
{
max_pos=cur_pos-cur_count;
max_count=cur_count;
}
*temp_output=(char *)malloc(max_count*sizeof(char)+1);
if(max_count==0)
**temp_output='\0';
else
strncpy(*temp_output,intputstr,cur_count);
return max_count;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-07-20
#include <stdio.h>

#include <string.h>

int main()

{

char str[50]={0};

scanf("%s",str);

int len=strlen(str);

int i=0,n=0,max=0,temp=0;

for (i=0; i<len; i++)

{

if ((str[i]>='0'&&str[i]<='9')&&(str[i+1]>='0'&&str[i+1]<='9')&&str[i+1]==str[i]+1)

{

n++;

}

else

{

if (n>max)

{

max=n;

temp=i-n;

n=0;

}

}

}

for (i=temp; i<=temp+max ; i++)

{

printf("%c",str[i]);

}

printf("\n");

return 0;

}
第2个回答  2013-05-06
先找出所有的数字
判断每个数字高位是不是小于次高位,依次类推追问

这个我也知道啊 我想要的是代码实现好不 自己代码实现的时候,总是差点意思

相似回答