【问题标题】:C language wrong output...pascal triangle using 2d arraysC语言错误输出...使用二维数组的帕斯卡三角形
【发布时间】:2018-11-06 22:23:24
【问题描述】:

所以这是我使用二维数组打印帕斯卡三角形的代码,但它没有给我想要的输出,我无法确定逻辑/代码有什么问题。

#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;
}

【问题讨论】:

  • 使用了num 的什么值?预期的输出是什么?
  • 哎呀,我的回答是评论,请求输出。
  • @Bwebb 至少那个“错误”让你超过了 200 ;)
  • @Swordfish 仍然会修复这些家伙的作业,因为这个答案并不算数:p
  • 我用5作为num的值

标签: c multidimensional-array output pascals-triangle


【解决方案1】:

else 之后的语句周围没有大括号,因此当if 语句的条件为真时,您似乎将加倍printf()

我将源代码复制到 codechef.com/ide 并将 io 的 num 更改为仅分配给 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");

【讨论】:

  • @AnasAziz 对我来说看起来不错,你对花括号有什么误解?当您说某事没有产生您期望的结果时,您应该真正弄清楚它在做什么以及在此处提问时您期望什么,这样回答起来会容易得多。
  • @AnasAziz 我会继续发表评论,直到您获得所需的答案,请确保在回答您提出的问题时将评论标记为已接受。
【解决方案2】:

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

#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

【讨论】:

  • 如果有人知道如何正确设置间距,请发表评论;)
  • 我赞成它,因为它的间距 :) printf("%*i" 用两个参数做了什么?我认为它会使它们相乘,但是当我测试它时,它只打印了第二个参数。
  • @Bwebb 第一个参数max_digits 用于* 并指定要打印的字段的with。
  • 我不熟悉 * 作为 printf 格式说明符,我似乎无法找到任何关于它的信息,您能否链接我以进一步阅读。我认为 printf 是一个 va_list 并且每个参数都需要一个 % 否则它只会被弹出堆栈并且什么都不做(最好的情况)
  • @Bwebb en.cppreference.com/w/c/io/fprintf - 阅读参​​数format的描述。 “介绍性 % 字符”之后的第二个“(可选)”。
猜你喜欢
  • 2015-03-19
  • 2011-07-07
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
相关资源
最近更新 更多