C++ 递归

int jiecheng(int num)//阶乘递归
 {
     if (num != 0)
         num--;
     else
         return 1;
     return (num + 1) * jiecheng(num);
 }
 int rabbit(int num)//斐波那契数列递归
 {
     if ((num == 1) || (num == 2))
         return 1;
     return rabbit(num - 1) + rabbit(num - 2);
 }
 int maxyue(int m, int n)//最大公约数递归
 {
     if (n == 0)
         return m;
     else
         return maxyue(n, m % n);
 }
int divide_search(int a[],int L,int H,int valu)//在有序数组里查找元素的二分法递归
 {
     if ((L > H)||(valu>a[H])||(valu a[mid])
         return divide_search(a, mid + 1, H, valu);
     else if (valu < a[mid])
         return divide_search(a, L, mid - 1, valu);
     else
         return mid;
 }

cout << “阶乘:”<< jiecheng(6)<” << endl;
cout <<“***************************************”<<endl;
cout << “兔子数列:” ;
for (int i = 1; i < 7; i++)
cout<“rabbit(i)” << endl;
cout<<endl<<“**************************************”<<endl;
cout << “最大公约数:” << maxyue(15, 35) << endl;
cout <<“***************************************”<<endl;
int g[] = { 1,3,4,5,6,8,17 };
cout << divide_search(g,0,6,4) << endl;

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注