递归 栈溢出,该怎么处理

递归栈溢出#include iostreamusing namespace stdint yh(int m,int n){yh(1,1)1if(n1||nm) retu

递归 栈溢出
#include <iostream>
using namespace std;
int yh(int m,int n)
{
 
yh(1,1)==1;
if(n==1||n==m) return 1;
if(n=0) return 0;
else return yh(m-1,n-1)+ yh(m-1,n);
}
int main()
{
int m,n,i;
yh(i,n);
cin>>m;
for(i=1;i<m+1;i++)
for(n=1;n<=m;n++)
cout<<yh(i,n)<<" ";
return 0;
}

栈溢出 求修改 输出杨辉三角

[解决办法]
我替你改好了。

这个程序的主要问题有:
1. 程序开始,在尚未对i和n赋值的情况下,直接调用yh(i,n),程序崩溃也就不能理解了

2. 没有考虑函数yh(m,n)中的m和n的取值范围
首先,n应该大于等于1,m应该大于等于0,且小于等于n。
另外,没有考虑整数溢出的情况,当n=34,C(17,34)=2333606220,超过32bit整数的最大值2147483647,故限制n的值不能大于34

3. 杨辉三角形的递推也不对,我将“return yh(m-1,n-1)+ yh(m-1,n); ”改成“return yh(m,n-1)+ yh(m-1,n-1);”就可以正确计算了。


C/C++ code
#include <iostream>using namespace std;int yh(int m,int n){    if ( n<=0)    {        printf("error\n");        return 0;    }    else if (n>=34)    {        printf("overflow\n");        return 0;    }    else if (m>n || m<0)    {        printf("overflow\n");        return 0;    }        if (m==0 || m==n)  // if n==1, then C(n,0)=C(n,1)=1;        return 1;    else         //return yh(m-1,n-1)+ yh(m-1,n); //这个公式不对        return yh(m,n-1)+ yh(m-1,n-1);}int main(){    int m,n,i;    // yh(i,n);  //i,n的值没有输入,是一个随机数,怎么用调用yh呢?    //cin>>m;    //C(17,34)=2333606220,超过32bit整数的最大值2147483647,故限制n的值不能大于34    for (n=1;n<34;n++)    {        for (m=0;m<=n;m++)            printf("C(%d,%d)=%d\n",m,n,yh(m,n));    }    return 0;}