/*
写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。
例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789。
//*/
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
/*
int main()
{
char a[] = "fsfd131ASDDVASdafdg13546sdf53454fd121566s12f1546412165sdfsdggf";
cout << a << endl;
int ps, pe, ts, te;
int sz = sizeof(a);
ts = -1; // 临时变量,存储当前数字字串的起始位置
te = -1; // 临时变量,存储当前数字字串的末位置
ps = -1; // 用于存储当前最长数字字串的起始位置
pe = -1; // 用于存储当前最长数字字串的末位置
int pos = 0;
while(pos < sz -2){
while(!(a[pos] >= '0' && a[pos] <= '9') && pos < sz -2){
++pos;
}
if(a[pos] >= '0' && a[pos] <= '9'){
ts = pos;
++pos;
while(pos < sz -2 && a[pos] >= '0' && a[pos] <= '9' && pos < sz -2){
++pos;
}
te = pos -1 ;
}
if((te-ts) > (pe - ps)){
ps = ts;
pe = te;
}
}
if(pe-ps>0){
int i;
char* res = new char[pe-ps +2];
for(i = ps; i <= pe; ++i){
res[i-ps] = a[i];
}
res[i-ps] = '\0';
cout << "longgest number string: " << res << endl;
}else{
cout << "there is not character satisfied the requirements!" <<endl;
}
return 0;
}
//*/
/*
求最大连续递增数字串(如"ads3sl456789DF3456ld345AA"中的"456789")。
*/
int main()
{
//char a[] = "fsfd131ASDDVASdafdg13546sdf53454fd121566s12f1546412165sdfsdggf";
char a[] = "ads3sl456789DF3456ld345AA";
cout << a << endl;
int ps, pe, ts, te;
int sz = sizeof(a);
ts = -1; // 临时变量,存储当前数字字串的起始位置
te = -1; // 临时变量,存储当前数字字串的末位置
ps = -1; // 用于存储当前最长数字字串的起始位置
pe = -1; // 用于存储当前最长数字字串的末位置
int pos = 0;
while(pos < sz -2){
while(!(a[pos] >= '0' && a[pos] <= '9') && pos < sz -2){
++pos;
}
if(a[pos] >= '0' && a[pos] <= '9'){
ts = pos;
++pos;
while(pos < sz -2 && a[pos] >= '0' && a[pos] <= '9' && pos < sz -2){
if( pos == 0 || (a[pos] > a[pos -1])){ // 此处判断是否递增
++pos;
}else{
break;
}
}
te = pos -1 ;
}
if((te-ts) > (pe - ps)){
ps = ts;
pe = te;
}
}
if(pe-ps>0){
int i;
char* res = new char[pe-ps +2];
for(i = ps; i <= pe; ++i){
res[i-ps] = a[i];
}
res[i-ps] = '\0';
cout << "longgest number string: " << res << endl;
}else{
cout << "there is not character satisfied the requirements!" <<endl;
}
return 0;
}
最长数字字串
最新推荐文章于 2021-08-19 12:03:36 发布