C语言错误输出...使用二维数组的pascal三角形

C language wrong output...pascal triangle using 2d arrays

所以这是我使用二维数组打印帕斯卡三角形的代码,但它没有给我想要的输出,我无法确定 logic/code.

有什么问题
#include <stdio.h>

int main()
{
    int num, rows, col, k;
    printf("Enter the number of rows of pascal triangle you want:");
    scanf("%d", &num);
    long a[100][100];

    for (rows = 0; rows < num; rows++)
    {
        for (col = 0; col < (num - rows - 1); col++)
            printf(" ");

        for (k = 0; k <= rows; k++)
        {
            if (k == 0 || k == rows)
            {
                a[rows][k] = 1;
                printf("%ld", a[rows][k]);
            }
            else
                a[rows][k] = (a[rows - 1][k - 1]) + (a[rows - 1][k]);
                printf("%ld", a[rows][k]);
        }
        printf("\n");
    }
    return 0;
}

您在 else 之后的语句周围没有大括号,因此当 if 语句的条件为是的。

我将源代码复制到 codechef 中。com/ide 并将 num 的 io 更改为刚分配给 6,这产生了以下输出:

Enter the number of rows of pascal triangle you want:     
     11
    1111
   11211
  113311
 1146411
1151010511

它看起来像你的收盘价,但你想要 1、11、121、1331 等,对吗?

包装 else 案例产生以下输出:

 if (k == 0 || k == rows)

   {
        a[rows][k] = 1;
        printf("(%ld)", a[rows][k]);
    }

    else{// START OF BLOCK HERE
        a[rows][k] = (a[rows - 1][k - 1]) + (a[rows - 1][k]);
        printf("(%ld)", a[rows][k]);
    }//END OF BLOCK HERE, NOTE THAT IT INCLUDES THE PRINT IN THE ELSE CASE NOW

OUTPUT:
    Enter the number of rows of pascal triangle you want:
         (1)
        (1)(1)
       (1)(2)(1)
      (1)(3)(3)(1)
     (1)(4)(6)(4)(1)
    (1)(5)(10)(10)(5)(1)

但我添加了 () 以使其更清楚。我还在第一个请求 num 值的 printf 的末尾添加了一个“/n”,这样第一行就换行了。

printf("Enter the number of rows of pascal triangle you want:\n");

您可以在不使用任何数组的情况下做到这一点:

#include <stdlib.h>
#include <stdio.h>

int num_digits(int number)
{
    int digits = 0;
    while (number) {
        number /= 10;
        ++digits;
    }
    return digits;
}

unsigned max_pascal_value(int row)
{
    int result = 1;
    for (int num = row, denom = 1; num > denom; --num, ++denom)
        result = (int)(result * (double)num / denom );  
    return result;
}

int main()
{
    printf("Enter the number of rows of pascals triangle you want: ");
    int rows;
    if (scanf("%d", &rows) != 1) {
        fputs("Input error. Expected an integer :(\n\n", stderr);
        return EXIT_FAILURE;
    }

    int max_digits = num_digits(max_pascal_value(rows));
    for (int i = 0; i <= rows; ++i) {
        for (int k = 0; k < (rows - i) * max_digits / 2; ++k)
            putchar(' ');

        int previous = 1;
        printf("%*i ", max_digits, previous);

        for (int num = i, denom = 1; num; --num, ++denom) {
            previous = (int)(previous * (double)num / denom );
            printf("%*i ", max_digits, previous);
        }
        putchar('\n');
    }
}

输出:

Enter the number of rows of pascals triangle you want: 15
                                  1
                                1    1
                              1    2    1
                            1    3    3    1
                          1    4    6    4    1
                        1    5   10   10    5    1
                      1    6   15   20   15    6    1
                    1    7   21   35   35   21    7    1
                  1    8   28   56   70   56   28    8    1
                1    9   36   84  126  126   84   36    9    1
              1   10   45  120  210  252  210  120   45   10    1
            1   11   55  165  330  462  462  330  165   55   11    1
          1   12   66  220  495  792  924  792  495  220   66   12    1
        1   13   78  286  715 1287 1716 1716 1287  715  286   78   13    1
      1   14   91  364 1001 2002 3003 3432 3003 2002 1001  364   91   14    1
    1   15  105  455 1365 3003 5005 6435 6435 5005 3003 1365  455  105   15    1