2017-06-13 59 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; 
}