帕斯卡三角形返回无意义值
这是我前一段时间分配的一个家庭作业项目......我已经成功地独自完成了这一步,我剩下的唯一问题是(我相信)数据类型和溢出的问题。
我尝试更改为无符号和双精度,并且代码符合要求并且仍然接受终端中的输入,但之后似乎挂起......没有打印任何内容,看起来像是陷入了循环。
这是代码...
/* 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正遭受整数溢出之苦。您可能需要找到一种不同的算法方法以避免计算大量数字。
为了回答有关换行符的其他观点,您在 print 语句中使用printf("\n"); ,以便整行以换行符终止。
\n
显式打印换行符。把它去掉,你就会得到打印在一行上的答案。您可能希望在最后包含一个最终的其他一些观察结果:
return 0;
- 控件将退出if
块的底部以及第二个块(应该是唯一的)return 0;
并且不会引起任何问题。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 finalprintf("\n");
at the end so the whole line is terminated in a newline.Some other observations:
return 0;
- the control will drop out ofthe bottom of the
if
block and on to the second (should be only)return 0;
and not cause any problems.scanf
; you can pass&n
directly.对于垃圾,您很可能会遇到整数溢出,也就是说,您的计算值对于
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!.将
scanf(" %i", nPtr);
更改为并将
printf("\n %i", ans);
更改为在一行上打印输出,请使用
:您正在使用
gcc
,请打开警告,即使用-Wall
。Change
scanf(" %i", nPtr);
toand
printf("\n %i", ans);
toto get printout on one line, use:
If you are using
gcc
, turn on warnings, i.e. use-Wall
.