帕斯卡三角形返回无意义值

发布于 2024-12-07 15:11:28 字数 1486 浏览 1 评论 0原文

这是我前一段时间分配的一个家庭作业项目......我已经成功地独自完成了这一步,我剩下的唯一问题是(我相信)数据类型和溢出的问题。

我尝试更改为无符号和双精度,并且代码符合要求并且仍然接受终端中的输入,但之后似乎挂起......没有打印任何内容,看起来像是陷入了循环。

这是代码...

    /* pascaltri.c
     * A program that takes a single integer as input and returns the nth line of
     * Pascal's Triangle. Uses factorial() function to help find items of
     * individual entries on a given row.
     */
    #include <stdio.h>
    #include <stdlib.h>
    long factorial(long i) 
    {
        long fact = 1;

        while(i > 1)
        {
            fact = fact * i;
            i = i - 1;
        }
        return fact;
    }
    main(void)
    {   
        long n;
        long *nPtr;
        nPtr = &n;
        scanf(" %i", nPtr); 
        if (n >= 0)
        {
            long k;
            long *kPtr;
            kPtr = &k;                
            for(k = 0; k <= n; k++)
            {
                long ans;
                long *ansPtr;
                ansPtr = &ans;

                ans = factorial(n) / (factorial(k) * factorial(n - k));
                printf("\n %i", ans);
            }
            return 0;
        }
        return 0;
    }

它并不完美或漂亮,但它最多可以输入三角形的 13(即第 14 行)。除此之外,我开始收到乱码,甚至在整个返回中散布着负值……更大的值会破坏代码,除了退出错误消息之外什么也不返回。

关于如何纠正这个问题有什么想法吗?我盯着屏幕看了太久才真正看到任何东西。另外,这不是必需的,但我想将返回值打印在一行上,而不是用换行符分隔它们。

1 5 10 10 5 1

最简单的方法是在计算值时将其加载到数组中,然后打印该数组吗?或者是否有一种内置方法可以告诉 print 语句仅出现在一行上?

This is a homework project I was assigned some time ago... I've been successful in getting this far on my own, and the only hiccup I have left is (I believe) an issue with data types and overflow.

I've tried changing over to unsigned and double, and the code complies and still accepts input in the terminal, but it seems to hang up after that... nothing is printed and it looks like it's caught in a loop.

Here is the code...

    /* pascaltri.c
     * A program that takes a single integer as input and returns the nth line of
     * Pascal's Triangle. Uses factorial() function to help find items of
     * individual entries on a given row.
     */
    #include <stdio.h>
    #include <stdlib.h>
    long factorial(long i) 
    {
        long fact = 1;

        while(i > 1)
        {
            fact = fact * i;
            i = i - 1;
        }
        return fact;
    }
    main(void)
    {   
        long n;
        long *nPtr;
        nPtr = &n;
        scanf(" %i", nPtr); 
        if (n >= 0)
        {
            long k;
            long *kPtr;
            kPtr = &k;                
            for(k = 0; k <= n; k++)
            {
                long ans;
                long *ansPtr;
                ansPtr = &ans;

                ans = factorial(n) / (factorial(k) * factorial(n - k));
                printf("\n %i", ans);
            }
            return 0;
        }
        return 0;
    }

It's not perfect or pretty, but it works up to an input of 13 (that is, row 14) of the triangle. Beyond that I start getting gibberish and even negative values sprinkled throughout the returns... much larger values break the code and return nothing but an exit error message.

Any ideas on how I can correct this problem? I've been staring at the screen for much to long to really see anything myself. Also, it's not essential, but I would like to print my return values on one line, rather than having them separated by a newline character.

1 5 10 10 5 1

Would the easiest way be to load the values into an array as they are computed, and then print the array? Or is there a built-in way I can tell the print statement to occur on only one line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北座城市 2024-12-14 15:11:28

您正遭受整数溢出之苦。您可能需要找到一种不同的算法方法以避免计算大量数字。

为了回答有关换行符的其他观点,您在 print 语句中使用 \n 显式打印换行符。把它去掉,你就会得到打印在一行上的答案。您可能希望在最后包含一个最终的printf("\n"); ,以便整行以换行符终止。

其他一些观察结果:

  • 您不需要第一个 return 0; - 控件将退出
    if 块的底部以及第二个块(应该是唯一的)
    return 0; 并且不会引起任何问题。
  • 您声明了 kPtr,但不在任何地方使用它。
  • 您不需要声明单独的变量 nPtr 来传递给 scanf;您可以直接传递 &n

You are suffering from integer overflow. You may need to find a different approach to the algorithm to avoid having to calculate the large numbers.

In answer to your other point about the newline, you are explicitly printing the newline with the \n in your print statement. Remove it, and you will get answers printed on one line. You probably want to inlucde a final printf("\n"); at the end so the whole line is terminated in a newline.

Some other observations:

  • You don't need the first return 0; - the control will drop out of
    the bottom of the if block and on to the second (should be only)
    return 0; and not cause any problems.
  • You're declaring kPtr but not using it anywhere
  • You don't need to declare a separate variable nPtr to pass to scanf; you can pass &n directly.
枕梦 2024-12-14 15:11:28

对于垃圾,您很可能会遇到整数溢出,也就是说,您的计算值对于 long 数据类型来说变得太大。您应该通过计算阶乘函数来纠正它,而不需要显式计算 n!。

For the garbage, you are most likely running into an integer overflow, that is, your calculated values become too large for the long data type. You should correct it by calculating your factorial function without explicitely calculating n!.

浪推晚风 2024-12-14 15:11:28

scanf(" %i", nPtr); 更改为

scanf(" %ld", nPtr); 

并将 printf("\n %i", ans); 更改为

printf("\n %ld", ans);

在一行上打印输出,请使用

printf(" %ld", ans);

:您正在使用gcc,请打开警告,即使用-Wall

Change scanf(" %i", nPtr); to

scanf(" %ld", nPtr); 

and printf("\n %i", ans); to

printf("\n %ld", ans);

to get printout on one line, use:

printf(" %ld", ans);

If you are using gcc, turn on warnings, i.e. use -Wall.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文