简体   繁体   English

Pascal在c中的三角形,具有递归函数

[英]Pascal's triangle in c with recursive functions

Hi this is my code for calculating pascal triangle but it runs error : has stopped working... why ? 嗨这是我的计算pascal三角形的代码,但它运行错误:已停止工作...为什么? i think its error is in paskal function 我认为它的错误在于paskal功能

#include <stdio.h>

long paskal(int,int);

int main (void)
{
    int  n = 0 ;
    int m = 0 ;
    int k  = 0 ;

    scanf("%d" , &n);

    for(k = 1  ; n >= k ;   )
    {
        for(  m = 1 ; k >= m   ; m++ )
        {
            long f = paskal(k , m ) ;
            printf("%ld" , f);
        }
        printf("\n");
        k++;
    }

     return 0; 
}

long paskal( int n , int i ) 
{
    if(n == 1 && i == 1 )
        return 1 ; 
    else
        return paskal(n-1,i) + paskal(n-1,i-1);
}

The limit-conditions are not correct. 限制条件不正确。

The correct way to put the limit conditions is 确定限制条件的正确方法是

if(n == 1 || i == 1 )
    return 1 ; 
else
    ....

It looks like you just need to account for cases where i>n and where i or n = 0: 看起来你只需要考虑i> n和i或n = 0的情况:

long paskal( int n , int i ) 
{
if(i<0 || n<0 || i>n) return 0;
if(n == 1)
    return 1 ; 
else

    return paskal(n-1,i) + paskal(n-1,i-1);
}

There are few problems with your code like the recursion termination condition: 您的代码几乎没有像递归终止条件那样的问题:

if(n == 1 && i == 1 )

which is not correct and lead to recurse the function forever and ultimately the program will terminate abnormally. 这是不正确的,导致永远递归功能,最终程序将异常终止。

Moreover, if you fix this condition, you are not going to get the Pascal triangle as output because both the for loops in main() is initializing the loop variable with 1 which will eat up the first column of every row of pascal triangle. 此外,如果你修复了这个条件,你就不会得到Pascal三角形作为输出,因为main()for循环都是用1初始化循环变量,这将占用每行pascal三角形的第一列。

For the terminating condition of recursive function paskal() - 对于递归函数paskal()的终止条件 -

In the function long paskal( int n , int i ) , the n is representing the row whereas the i represent the column in that particular row and pascal() function is supposed to calculate the element at a particular location. 在函数long paskal( int n , int i )n表示行,而i表示该特定行中的列,而pascal()函数应该计算特定位置的元素。

A couple of points, in Pascal triangle: Pascal三角形中的几点:

  1. The leftmost and rightmost element of every row is 1 . 每行的最左边和最右边的元素是1

  2. Every row contains the number of columns same as the row number. 每行包含与行号相同的列数。

So, I can say - 所以,我可以说 -

if(n == i || i == 0) //i.e. if ((row == col) || (col == 0)) return 1;
    return 1;

could be terminating condition of the recursive function. 可能是递归函数的终止条件。

Collectively, your program will be something like this: 总的来说,你的程序将是这样的:

#include <stdio.h>

long paskal(int, int);

int main() {
   int n = 0;
   int m = 0;
   int k = 0;
   int s = 0;
   printf ("Enter number of rows:\n");
   scanf("%d", &n);

   for(k = 0; n >= k; k++) {
            for(s = 0; s < n-k; s++) //Add spaces before each row
                    printf(" ");
            for(m = 0; k >= m; m++) {
                    long f = paskal(k, m);
                    printf("%ld ", f);
            }
            printf("\n");
    }
    return 0;
}

long paskal(int n, int i) {
        if(n == i || i == 0)
                return 1;
        else
                return paskal(n-1, i) + paskal(n-1, i-1);
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM