正在读 Haskell教程, 这一页 里谈到了以递归的形式来实现一个 maximum函数,其作用是返回一个列表中最大的那个元素的值。
看完之后 ,自己忍不住用C++实现了一下这个递归算法 ,巩固一下对递归的理解 。算法在那篇教程里已经说得狠清楚了 ,对于元素个数大于 1的列表,把它的第一个元素与 后面所有元素组成的子列表的最大值 比较,哪个大一些,就会作为最终的最大值。在计算后面所有元素组成的子列表的最大值的时候 ,就是发生递归的时候。
以下是代码 ,这里用到了 QList,是因为本座对于 QT中的模板库更熟悉,对于 STL反而不那么熟悉 ,当然本座更没有心思去使用裸数组了 ,这里只是研究算法 。
#include <iostream>
#include <QList> //QList
#include <QtDebug> //qDebug
using namespace std ;
int MyMax( QList < int > TheList2Cpt)
{
int Result= 0 ; //结果。
if (TheList2Cpt.isEmpty()) //数组为空。
{
qDebug() << QObject ::tr( "The list is empty" ); //Debug.
Result= 0 ; //随意返回一个结果。可能这里抛个异常玩玩更合适。
} //if (TheList2Cpt.isEmpty()) //数组为空。
else if (TheList2Cpt.length()== 1 ) //只有一个元素,那么就是最大值。
{
Result=TheList2Cpt.at( 0 ); //第一个就是最大值。
} //if (TheList2Cpt.length()==1) //只有一个元素,那么就是最大值。
else //将第一个元素与之后的子数组的最大值比较。
{
int MxTl=MyMax(TheList2Cpt.mid( 1 )); //计算后面子数组的最大值。
if (TheList2Cpt.at( 0 ) > MxTl) //第一个元素比较大。
{
Result=TheList2Cpt.at( 0 ); //第一个就是最大值。
} //if (TheList2Cpt.at(0) > MyMax(TheList2Cpt.mid(1))) //第一个元素比较大。
else //后面的子数组的最大值比较大。
{
Result=MxTl; //后面的子数组的最大值就是整个数组的最大值。
} //else //后面的子数组的最大值比较大。
} //else //将第一个元素与之后的子数组的最大值比较。
qDebug() << __FILE__ << __LINE__ << __func__ << QObject::tr( "The list a s param et er :" ) << TheList2Cpt << QObject::tr("Maximu m of this l is t: " ) << Result; //Debug.
return Result;
} //int MyMax(QList<int> TheList2Cpt)
int main()
{
QList < int > TheList; //数组。
TheList << 0 << 9 << 4 << 4 << 2 << 7 << 2 << 0 << 1 << 3 << 3 << 3 << 0 << 6 ; //加入元素。
cout << MyMax(TheList) << endl;
return 0 ;
}
编译之后,运行 ,得到的输出,里面展示了MyMax函数被递归调用的过程:
调试开始
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (6) "Maximum of this list:" 6
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (0, 6) "Maximum of this list:" 6
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (3, 0, 6) "Maximum of this list:" 6
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (3, 3, 0, 6) "Maximum of this list:" 6
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (3, 3, 3, 0, 6) "Maximum of this list:" 6
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (1, 3, 3, 3, 0, 6) "Maximum of this list:" 6
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (0, 1, 3, 3, 3, 0, 6) "Maximum of this list:" 6
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (2, 0, 1, 3, 3, 3, 0, 6) "Maximum of this list:" 6
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (7, 2, 0, 1, 3, 3, 3, 0, 6) "Maximum of this list:" 7
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (2, 7, 2, 0, 1, 3, 3, 3, 0, 6) "Maximum of this list:" 7
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (4, 2, 7, 2, 0, 1, 3, 3, 3, 0, 6) "Maximum of this list:" 7
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (4, 4, 2, 7, 2, 0, 1, 3, 3, 3, 0, 6) "Maximum of this list:" 7
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (9, 4, 4, 2, 7, 2, 0, 1, 3, 3, 3, 0, 6) "Maximum of this list:" 9
../RecursiveMaximum/main.cpp 36 MyMax "The list as parameter:" (0, 9, 4, 4, 2, 7, 2, 0, 1, 3, 3, 3, 0, 6) "Maximum of this list:" 9
9
调试结束
年少金发美女的接吻
HxLauncher: Launch Android applications by voice commands