递归与循环

1. 例子:

#include <iostream>
using namespace std;

//循环实现累加:
int Sum(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
    {
        sum += i;
    }
    return sum;
}
//递归实现累加:
int RecursiveSum(int n)
{
    if (n == 0)
    { 
        return 0;

    } 
    else
    {
        return RecursiveSum(n - 1) + n;
    }
}

void main()
{
    int num;
    cout << "请输入要累加的数:";
    cin >> num;

    int sum1 = Sum(num);
    int sum2 = RecursiveSum(num);

    cout << "循环结果为:" << sum1 << endl;
    cout << "递归结果为:" << sum2 << endl;

    system("pause");
}

    

2. 递归转非递归:

#include <iostream>
#include <stack>

using namespace std;

//递归:
void Print1(int n) //打印1~n的数(顺序)
{
    if (n <= 0)
    {
        return;
    } 
    else
    {
        Print1(n - 1);        //交换这两行位置,则为逆序
        cout << n << "  " ;
    }
}

//非递归:
void RecursivePrint1(int n)
{
    stack<int> mystack;                        /* 1.初始化栈 */

L0:                                            /* 2.设入口标号 */
    if (n <= 0)                                /* 3.非递归调用和返回部分照搬 */
    {
        while (!mystack.empty())            /* 5.返回部分替换 */
        {
            cout << mystack.top() << "  " ;
            mystack.pop();
        }

        return ;
    }
    else                                    /* 4.递归调用Print1(n - 1),替换 */
    {
        mystack.push(n);
        n = n - 1;

        goto L0;
    }
}

//递归(逆序):
void Print2(int n) //打印1~n的数(顺序)
{
    if (n <= 0)
    {
        return;
    }
    else
    {
        cout << n << "  ";
        Print2(n - 1);        
    }
}
//非递归:
void RecursivePrint2(int n)
{
    stack<int> mystack;                            /* 1.初始化栈 */

L0:                                                /* 2.设入口标号 */
    if (n <= 0)                                    /* 3.非递归调用和返回部分照搬 */
    {
        return;
    }
    else                                        /* 4.递归调用Print2(n - 1),替换 */
    {
        mystack.push(n);    //吃一个,吐一个,逆序

        while (!mystack.empty())                /* 5.返回部分替换 */
        {
            cout << mystack.top() << "  ";
            mystack.pop();
        }

        n = n - 1;

        goto L0;
    }
}

void main()
{
    cout << "递归(顺序):" << endl;
    Print1(10);
    cout << "\n\n非递归(顺序):" << endl;
    RecursivePrint1(10);
        

    cout << "\n\n递归(逆序):" << endl;
    Print2(10);
    cout << "\n\n非递归(逆序):" << endl;
    RecursivePrint2(10);

    cin.get();
}

    

3. 斐波那契数列 递归转非递归:

#include <iostream>
#include <stack>

using namespace std;

//递归斐波那契:
int Fibonacci(int n)
{
    if (n == 1)
    {
        return 1;
    }
    else if (n == 2)
    {
        return 1;
    }
    else
    {
        return Fibonacci(n - 1) + Fibonacci(n - 2);//树状递归
    }
}

//(方法一)非递归斐波那契:
int RecursiveFibonacci1(int n)
{
    if (n==1)
    {
        return 1;
    }
    else if (n == 2)
    {
        return 1;
    }

    int f1, f2, f3;
    f1 = f2 = 1;
    for (int i = 2; i < n; i++)
    {
        f3 = f1 + f2;
        f1 = f2;
        f2 = f3;    //轮替
    }

    return f3;
}
//(方法二)非递归斐波那契:
int RecursiveFibonacci2(int n)
{
    if (n == 1)
    {
        return 1;
    }
    else if (n == 2)
    {
        return 1;
    }

    stack<int> mystack;

    int f1, f2, f3;
    f1 = f2 = 1;
    int i = 2;

L0:
    if (i < n)
    {
        mystack.push(f1);
        mystack.push(f2);
        f3 = 0;
        while (!mystack.empty())
        {
            f3 = f3 + mystack.top();
            mystack.pop();
        }
        f1 = f2;
        f2 = f3;
        i++;

        goto L0;
    }

    return f3;
}

void main()
{
    int num;
    cout << "斐波那契个数:";
    cin >> num;

    cout << "\n\n递归斐波那契:" << endl;
    for (int i = 1; i <= num; i++)
    {
        cout << Fibonacci(i) << "  ";
    }

    cout << "\n\n(方法一)非递归斐波那契:" << endl;
    for (int i = 1; i <= num; i++)
    {
        cout << RecursiveFibonacci1(i) << "  ";
    }

    cout << "\n\n(方法二)非递归斐波那契:" << endl;
    for (int i = 1; i <= num; i++)
    {
        cout << RecursiveFibonacci2(i) << "  ";
    }


    cout <<"\n\n";
    system("pause");
}

    

 

posted @ 2018-08-29 21:19  博观&约取  阅读(245)  评论(0编辑  收藏  举报