• 递归在示例中的应用


    1.递归和非递归分别实现求第n个斐波那契数。

    方法1

    #include <stdio.h>

    #include <windows.h>

    #pragma waring (disable:4996)

    int factorial(int k)

    {

        if(k<=1)

        return 1;

        else

        return factorial(k-2)+factorial(k-1);

    }

    int main ()

    {

        int n=0;

        printf("求第几个的斐波那契数 ");

        scanf("%d",&n);

        int ret=factorial(n);

        printf("the namber is %d ",ret);

        system("pause");

        return 0;

    }

    方法2

    #include <stdio.h>

    #include <windows.h>

    #pragma waring (disable:4996)

    int fun(int n)

    {

        int p=1;

        int pp=1;

        int next=1;

        while(n-->2)

        {

            p=pp;

            pp=next;

            next=p+pp;

         }

         return next;

    }

    int main ()

    {

        int n=0;

        printf("求第几个的斐波那契数 ");

        scanf("%d",&n);

        int ret=fun(n);

        printf("the namber is %d ",ret);

        system("pause");

        return 0;

    }

    2.编写一个函数实现n^k,使用递归实现
    分析:n的k次=n*n*n*n*……*n(k个n),用递归的思想来讲,就等于n*(n的(k-1)次方)
    #include <stdio.h>
    #include <windows.h>
    #pragma waring (disable:4996)
    int fun(int n,int k)
    {
        if(k==0)
            return 1;
        else if(k==1)
            return n;
        else
            return n*fun(n,k-1);
    }
    int main ()
    {
        int n=0;
        int k=0;
        printf("求n的k次方 ");
        scanf("%d%d",&n,&k);
        printf("the namber is %d ",fun(n,k));
        system("pause");
        return 0;
    }
    3. 写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和,例如,调用DigitSum(1729),则应该返回1+7+2+9,它的和是19
    #include <stdio.h>
    #include <windows.h>
    #pragma waring (disable:4996)
    int DigitSum(int n)
    {
        if(n>9)
        {
            return     n%10+DigitSum(n/10);
        }
    }
    int main ()
    {
        int n=0;
        printf("求输入n: ");
        scanf("%d",&n);
        printf("the namber is %d ",DigitSum(n));
        system("pause");
        return 0;
    }
    4. 编写一个函数reverse_string(char * string)(递归实现)
    实现:将参数字符串中的字符反向排列。
    要求:不能使用C函数库中
    1.
    #include <stdio.h>
    #include <windows.h>
    #include <assert.h>
    #pragma waring (disable:4996)
    void reverse_string(const char * const string)
    {
        assert(string);
        if(*string=='')
        return ;
        reverse_string(string+1);
        putchar(*string);
    }
    int main (void)
    {
        char string[80];
        gets(string);
        reverse_string(string);
        system("pause");
        return 0;
    }
    5.递归和非递归分别实现strlen
    1.
    #include <stdio.h>
    #include <windows.h>
    #pragma waring (disable:4996)
    //.递归实现strlen
    int  My_Strlen(const char *p)
    {
       if(*p=='')
         return 0;
       else
         return 1+My_Strlen(p+1);
    }
    int main ()
    {
        char *p="abcdef";
        int len=My_Strlen(p);
        printf("%d ",len);
        system("pause");
        return 0;
    }
    2.#include <stdio.h>
    #include <windows.h>
    #pragma waring (disable:4996)
    //.非递归实现strlen
    int  My_Strlen(const char *p)
    {
        int count=0;
        if(*p=='')
         return 0;
         else
        while(*p)
         {
             p++;
             count++;
         }
         return count;
    }
    int main ()
    {
        char *p="abcdef";
        int len=My_Strlen(p);
        printf("%d ",len);
        system("pause");
        return 0;
    }
    6.递归和非递归分别实现求n的阶乘
    1.#include <stdio.h>
    #include <windows.h>
    #pragma waring (disable:4996)
    //用递归实现求n的阶乘
    int fun(int n)
    {
        if(n<=1)
        return 1;
        else
        return n*fun(n-1);
    }
    int main ()
    {
        int n=0;
        scanf("%d",&n);
        int ret=fun(n);
        printf("the namber is %d ",ret);
        system("pause");
        return 0;
    }
    2.
    #include <stdio.h>
    #include <windows.h>
    #pragma waring (disable:4996)
    //用非递归实现求n的阶乘
    int fun(int n)
    {
        int sum=1;
        if(n<=1)
        return 1;
        else
        {
             while(n)
           {
             sum=sum*(n--);
           }
            return sum;
        }
       
    }
    int main ()
    {
        int n=0;
        scanf("%d",&n);
        int ret=fun(n);
        printf("the namber is %d ",ret);
        system("pause");
        return 0;
    }
    7.递归方式实现打印一个整数的每一位
    #include <stdio.h>
    #include <windows.h>
    #pragma waring (disable:4996)
    //递归方式实现打印一个整数的每一位
    void fun(int n)
    {
       if(n>9)
       {
           fun(n/10);
       }
       printf("%d ",n%10);
    }
    int main ()
    {
        int n=0;
        scanf("%d",&n);
       fun(n);
        system("pause");
        return 0;
    }
  • 相关阅读:
    类的继承
    面向对象的编程
    Python的模块
    ES6_12_Set和Map数据结构以及for of循环
    ES6_11_字符串、数值、数组、对象扩展
    ES6_09_Generator函数
    ES6_08_Iterator遍历器
    ES6_07_Symbol属性
    ES6_05_三点运算符和形参默认值
    Sqlstate解释
  • 原文地址:https://www.cnblogs.com/xjq6898/p/7790093.html
Copyright © 2020-2023  润新知