您的位置:首页 > 其它

最大连续递增数字串

2012-09-06 19:39 92 查看
题目:求最大连续递增数字串(如“ads3sl456789DF3456ld345AA”中的“456789”。

答:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

//求最大连续递增数字串
string FindMaxIncreNumberSeq(string str)
{
if ("" == str || str.length() <= 0)
{
return NULL;
}
int maxlength = 0;
int start = -1;
int count = 0;
int pos = -1;
bool isbegin = true;
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
if (isbegin)
{
pos = i;
isbegin = false;
count = 1;
}
else if (str[i] - str[i - 1] == 1)
{
count++;
}
else
{
if (maxlength < count)
{
maxlength = count;
start = pos;
}
pos = i;
count = 1;
}

if (maxlength < count)
{
maxlength = count;
start = pos;
}
}
else
{
if (maxlength < count)
{
maxlength = count;
start = pos;
}
isbegin = true;
}
}
return str.substr(start, maxlength);
}

int _tmain(int argc, _TCHAR* argv[])
{
string str = "23456789ads3sl456789DF012341234567893456ld345AA345678";
cout<<"字符串:"<<str<<endl;
cout<<"最大连续递增数字串:";
cout<<FindMaxIncreNumberSeq(str);

cout<<endl;
return 0;
}


运行界面如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: