递归一定要用if, else, switch, case或三目运算符吗? 非也, 我们来看一个间接递归的程序:

#include <iostream>
using namespace std;

typedef int (*pFun)(int n);

int f0(int n)
{
	return 0;
}

int f1(int n)
{
	pFun fun[2] = {f0, f1};
	return fun[!!n](n - 1) + 1;
}

int main()
{
	int i = 0;  
    for(i = 0; i < 10; i++)  
    {  
        cout << f1(i) << endl;  
    }  

	return 0;
}
         结果:

1
2
3
4
5
6
7
8
9
10



本文转载:CSDN博客