C/C++笔试准备(1)
2015-09-26 10:47
316 查看
题目:用递归的算法实现这样一个函数,计算一个字符串最大连续相同字符数,输入aaabbc,输出3;输入bbc,输出2
递归的考虑:
1 有递归循环退出的条件;
2 写出子过程执行函数;
3 递归调用子过程;
勉强实现功能。。。。
#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 递归调用子过程;
勉强实现功能。。。。
相关文章推荐
- C语言中 .h文件和.c文件的区别
- C语言复习day2
- c的free和c++的delete的区别
- C语言里面 sizeof 的使用
- c/c++程序调试
- C++中的头文件和源文件
- C语言复习day1
- 技术面试(C++)
- C++学习篇——构造函数与析构函数
- C++ 的构造/析构/赋值/拷贝函数比较
- C++拷贝构造函数详解
- eclipse 配置c++开发环境
- C++ | STL 概述
- C++ | 段错误
- c++设计模式----迭代器模式iterator
- 链表的多项式加法链表的多项式加法
- C++ | RAII 机制
- 【转】探索C++的秘密之详解extern
- C/C++中“#”和“##”的作用和用法
- C++学习记录