您的位置:首页 > 编程语言 > C语言/C++

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

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

#include <iostream>
using namespace std;
void countCountinue(const char a[], int &count,int tmpCount,int curIndex)
{
int nextIndex = curIndex+1;
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 = 1;
if(tmpCount >= count)
count = tmpCount;
countCountinue(a,count,tmpCount,curIndex);
}
}

int main()
{
char a[] = "aaaabbcc3cccaaadadasexcc";
int count = 0;
int tmpCount = 1;
int curIndex = 0;
countCountinue(a,count,tmpCount,curIndex);
cout<<count<<"   "<<strlen(a);
}


递归的考虑:

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

2 写出子过程执行函数;

3 递归调用子过程;

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