2017-06-13 61 views
3

也有类似的问题,但没有人帮助我找到我正在寻找的答案,所以我来请求帮助!在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有东西吗?

任何指导,非常感谢。

+3

您必须同时打印第1列和第11列。 – tilz0R

+3

'13!'不等于'1932053504' – Stargateur

+1

@Stargateur - 看起来像整数溢出给我。 '13! == 0x17328CC00'。而'x7328CC00 == 1932053504'。 OP需要从“int”切换到“long long”来进行所有的计算,并使用'%ll'作为格式参数而不是'%d'。 – selbie

回答

5

打印列在一起,像

// Using long long to allow big numbers and to prevent overflow. 
unsigned long long lngFactorial1 = 1; 
unsigned long long lngFactorial2 = 3628800;// 10! 
for (intIndex = 1; intIndex <= 10; intIndex++) { 
    lngFactorial1 *= intIndex; 
    lngFactorial2 *= intIndex + 10; 
    printf("%d! = %-20llu ; %d! = %-20llu;\n", intIndex, lngFactorial1, intIndex + 10, lngFactorial2); 
} 
+0

这个。它并没有跨过我的想法初始化第二个阶乘,输出为10.谢谢! – user3691838

1

为此,你需要给printf到这个样子

printf("%d! = %-20lu "%20d ! = %lu", intIndex, lngFactorialn, (intIndex + 10), lngFactorialn); 

,所以你需要计算这些第一,然后在同一时间打印出来。 我认为这应该只是一个for循环做的伎俩。

+0

您必须创建2个因子变量,因为电流取决于以前的因子。 – tilz0R

+0

我认为这是解决方案,因为,从1到10,第二从11到20,他需要这是一行,并知道这是作业,老师认为,学生应该想到的。 –

+0

但看看你的价值观。 11的值取决于前一个循环的值10。这意味着你的输出有错误的结果。 – tilz0R

2

一旦你使用换行符,你不能回到上一行(不改变它)。

如果你需要1!和11!在同一行中,它们必须打印在一行,然后换行符。因此,对于特定的迭代,我和(10 + i)阶乘将被打印。 (1 < = i < = 10)。

此外,对于存储12以上的阶乘,您将需要长数据类型。

+1

换行符不是“运算符”,它只是一个字符。我认为将其称为运营商有点令人困惑。 – unwind

+0

谢谢。它已更新。 –

0

其他答案提到你必须同时计算每列中的相同行。这是我的解决方案,适用于各种列号:

#include <stdio.h> 
#include <string.h> 

void factorial_in_columns(int col_num, int width, int max_fact) { 
    int col_width = width/col_num; /* the width of a column */ 
    int fact_offset = max_fact/col_num; /* calculation distance */ 

    char col_buffer[col_width]; /* buffer of a column */ 
    char line_buffer[width + 1]; /* buffer of a line */ 

    long factorial; 
    int i, j, k, x, len; 

    for(i = 1; i <= fact_offset; ++i) { 
     for(j = 0; j < col_num; ++j) { 
      x = (i + (j * fact_offset)); /* current number */ 

      for(factorial = 1, k = 1; k <= x; ++k) 
       factorial *= k; 

      /* convert the result to string and append it to the line buffer */ 
      snprintf(col_buffer, col_width, "%d = %ld", x, factorial); 
      sprintf(line_buffer + (col_width * j), "%-*s", col_width, col_buffer); 
     }    
     printf("%s\n", line_buffer); 
    } 
} 

int main() { 
    factorial_in_columns(4, 120, 20); 
    return 0; 
}