首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C >> C 語言入門

C 語言階乘計算完整指南

     編輯:C 語言入門

數學定義

n! = 1 × 2 × ... × n,0! = 1。結果增長極快,13! 超過 32 位整數範圍,21! 超過 64 位。

迴圈實作(推薦)

#include <stdio.h>
#include <stdint.h>

int main() {
    int n;
    printf("請輸入非負整數 n:");
    if (scanf("%d", &n) != 1 || n < 0) return 1;

    uint64_t fact = 1; // 64 位無符號,支援至 20!
    for (int i = 2; i <= n; ++i) {
        fact *= i;
    }
    printf("%d! = %llu\n", n, fact);
    return 0;
}

遞迴實作(教學用)

uint64_t fact_rec(int n) {
    return (n <= 1) ? 1 : n * fact_rec(n - 1);
}
// 注意:遞迴深度受限於堆疊,n > 10^5 會棧溢位,階乘 n 通常很小故可接受

大整數階乘(任意精度)

C 無內建大整數,需手寫或用庫(GMP)。陣列模擬十進制位:

// 簡易大整數乘法:以 10^4 為基數
#define BASE 10000
int a[1000] = {1}; // 存儲結果,最低位在 a[0]
int len = 1;

void multiply(int n) {
    long long carry = 0;
    for (int i = 0; i < len; ++i) {
        carry += (long long)a[i] * n;
        a[i] = carry % BASE;
        carry /= BASE;
    }
    while (carry) {
        a[len++] = carry % BASE;
        carry /= BASE;
    }
}

void printBigInt() {
    printf("%d", a[len-1]);
    for (int i = len-2; i >= 0; --i) printf("%04d", a[i]);
    putchar('\n');
}

常見陷阱

總結

  • 迴圈版 O(n) 時間 O(1) 空間,生產首選。
  • 型別選擇關鍵:uint64_t 覆蓋 20!,更大需大整數。
  • 遞迴版適合教學展示數學定義,實務少用。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
陷阱現象對策
用 int 存結果n≥13 溢位,結果為負/亂碼用 uint64_t(至 20!)或大整數
未處理 n=0迴圈不執行,結果正確(初值 1)明確處理或依賴初值
輸入負數無意義輸入驗證 n >= 0
printf("%d", fact) 格式錯誤輸出錯誤%llu 對應 unsigned long long