题目:用递归的算法实现这样一个函数,计算一个字符串最大连续相同字符数,输入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 递归调用子过程;
勉强实现功能。。。。
放松不放纵--忙碌不盲目