Error[8]: Undefined offset: 4, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

1.什么是递归

程序调用自身的编程技巧叫做递归.。一个过程或函数在其定义或说明中直接或间接调用自身的一种方法

2.递归的两个必要条件

1.存在条件限制,当满足这个限制条件时候,递归就不再继续

2.每次递归调用之后越来越接近这个限制条件

递归错误1.

int main()
{
    printf("hello world");
    main();
    return 0;
}

没有终止条件,会造成死循环 栈溢出

什么是栈溢出?

栈区 (局部变量 函数形参) 堆区(动态内存分配的 malloc/free calloc realloc)

静态区(全局变量 静态变量)

在每一次递归时都会重新在内存中创建新的空间 然后超过了最大的空间 导致栈溢出

3.写递归时的注意事项

1.不能死递归 都有跳出条件 每次递归逼近跳出条件

2.递归层次不能太深

练习1:

编写函数不允许创建临时变量 求字符串长度

int my_strlen(char* str)
{
    if (*str != 'int Factorial(int x)
{
    if (x<=1){
        return 1;
    }else{
        return x * Factorial(x - 1);
    }
}
int main()
{
    int n = 0;
    scanf("%d", &n);
    int sum = Factorial(n);
    printf("%d\n", sum);
    return 0;
}'){
        return 1 + my_strlen(str + 1);
    }else{
        return 0;
    }
}
int main()
{
    char arr[] = "bit";
    // 模拟实现strlen函数
    printf("%d\n", my_strlen(arr));
}

2.求n的阶乘

int add(int a)
{
    if (a <= 2){
        return 1;
    }else{
        return add(a - 1) + add(a - 2);
    }
}
int main()
{
    int a = 6;
    int sum = add(a);
    printf("%d\n", sum);
    return 0;
}

3.斐波那契数列

[+++]

 优化版:

int add(int n)
{
    int a = 1;
    int b = 1;
    int c = 1;
    while (n>2){
        c = a + b;
        a = b;
        b = c;
        n--;
    }
    return c;
}
int main()
{
    int a = 10;
    int sum = add(a);
    printf("%d", sum);
    return 0;
}

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
初始c语言函数递归_C_内存溢出

初始c语言函数递归

初始c语言函数递归,第1张

1.什么是递归

程序调用自身的编程技巧叫做递归.。一个过程或函数在其定义或说明中直接或间接调用自身的一种方法

2.递归的两个必要条件

1.存在条件限制,当满足这个限制条件时候,递归就不再继续

2.每次递归调用之后越来越接近这个限制条件

递归错误1.

int main()
{
    printf("hello world");
    main();
    return 0;
}

没有终止条件,会造成死循环 栈溢出

什么是栈溢出?

栈区 (局部变量 函数形参) 堆区(动态内存分配的 malloc/free calloc realloc)

静态区(全局变量 静态变量)

在每一次递归时都会重新在内存中创建新的空间 然后超过了最大的空间 导致栈溢出

3.写递归时的注意事项

1.不能死递归 都有跳出条件 每次递归逼近跳出条件

2.递归层次不能太深

练习1:

编写函数不允许创建临时变量 求字符串长度

int my_strlen(char* str)
{
    if (*str != 'int Factorial(int x)
{
    if (x<=1){
        return 1;
    }else{
        return x * Factorial(x - 1);
    }
}
int main()
{
    int n = 0;
    scanf("%d", &n);
    int sum = Factorial(n);
    printf("%d\n", sum);
    return 0;
}'){
        return 1 + my_strlen(str + 1);
    }else{
        return 0;
    }
}
int main()
{
    char arr[] = "bit";
    // 模拟实现strlen函数
    printf("%d\n", my_strlen(arr));
}

2.求n的阶乘

int add(int a)
{
    if (a <= 2){
        return 1;
    }else{
        return add(a - 1) + add(a - 2);
    }
}
int main()
{
    int a = 6;
    int sum = add(a);
    printf("%d\n", sum);
    return 0;
}

3.斐波那契数列

 

 优化版:

int add(int n)
{
    int a = 1;
    int b = 1;
    int c = 1;
    while (n>2){
        c = a + b;
        a = b;
        b = c;
        n--;
    }
    return c;
}
int main()
{
    int a = 10;
    int sum = add(a);
    printf("%d", sum);
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/728346.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存