也有类似的问题,但没有人帮助我找到我正在寻找的答案,所以我来请求帮助!在C中打印列
此外,这是作业,所以我正在寻找一个解释,而不仅仅是工作代码。
我需要输出两列或四列输出以显示数字1-20的阶乘。
我非常接近,我似乎无法弄清楚如何让这两个部分并排。
代码:
lngFactorial = 1;
// Factorials 1 - 10
for (intIndex = 1; intIndex <=10; intIndex += 1)
{
lngFactorial *= intIndex;
printf("%d! = %-20lu \n", intIndex, lngFactorial);
}
// Factorials 11 - 20
for (intIndex = 11; intIndex <= 20; intIndex += 1)
{
lngFactorial *= intIndex;
printf("%20d ! = %lu \n", intIndex, lngFactorial);
}
输出:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11 ! = 39916800
12 ! = 479001600
13 ! = 1932053504
14 ! = 1278945280
15 ! = 2004310016
16 ! = 2004189184
17 ! = 4006445056
18 ! = 3396534272
19 ! = 109641728
20 ! = 2192834560
那么,这4列,我需要以某种方式找出该打印
1! = 1 11! = 39961800
还是我只是失踪在我的printf有东西吗?
任何指导,非常感谢。
您必须同时打印第1列和第11列。 – tilz0R
'13!'不等于'1932053504' – Stargateur
@Stargateur - 看起来像整数溢出给我。 '13! == 0x17328CC00'。而'x7328CC00 == 1932053504'。 OP需要从“int”切换到“long long”来进行所有的计算,并使用'%ll'作为格式参数而不是'%d'。 – selbie