机器学习猪

--------一只帅气潇洒略带才气的猪

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

题目:用递归的算法实现这样一个函数,计算一个字符串最大连续相同字符数,输入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 递归调用子过程;

 

勉强实现功能。。。。

posted on 2015-09-26 10:47  机器学习猪  阅读(298)  评论(0编辑  收藏  举报