C/C++笔试准备(1)

时间:2023-03-09 01:11:06
C/C++笔试准备(1)

题目:用递归的算法实现这样一个函数,计算一个字符串最大连续相同字符数,输入aaabbc,输出3;输入bbc,输出2

#include <iostream>
using namespace std;
void countCountinue(const char a[], int &count,int tmpCount,int curIndex)
{
int nextIndex = curIndex+;
if(curIndex > (int)strlen(a))
return; if(a[curIndex] == a[nextIndex])
{
curIndex = nextIndex;
tmpCount++;
if(tmpCount >= count)
count = tmpCount;
countCountinue(a,count,tmpCount,curIndex);
}else
{
curIndex = nextIndex;
tmpCount = ;
if(tmpCount >= count)
count = tmpCount;
countCountinue(a,count,tmpCount,curIndex);
}
} int main()
{
char a[] = "aaaabbcc3cccaaadadasexcc";
int count = ;
int tmpCount = ;
int curIndex = ;
countCountinue(a,count,tmpCount,curIndex);
cout<<count<<" "<<strlen(a);
}

递归的考虑:

1 有递归循环退出的条件;

2 写出子过程执行函数;

3 递归调用子过程;

勉强实现功能。。。。