4-10 階乘計(jì)算升級(jí)版

Attention: 如果喜歡我寫(xiě)的文章,歡迎來(lái)我的github主頁(yè)給star
Githubgithub.com/MuziJin

本題要求實(shí)現(xiàn)一個(gè)打印非負(fù)整數(shù)階乘的函數(shù)。

函數(shù)接口定義

void Print_Factorial ( const int N );

其中N是用戶(hù)傳入的參數(shù),其值不超過(guò)1000。如果N是非負(fù)整數(shù),則該函數(shù)必須在一行中打印出N!的值,否則打印“Invalid input”。

裁判測(cè)試程序樣例

#include <stdio.h>

void Print_Factorial ( const int N );

int main()
{
    int N;
                
    scanf("%d", &N);
    Print_Factorial(N);
    return 0;
}

/* 你的代碼將被嵌在這里 */

輸入樣例

15

輸出樣例

1307674368000

Code


void Print_Factorial ( const int N )
{
    int temp;
    int m = 0;  //要進(jìn)的數(shù) 
    int k = 1;  //當(dāng)前結(jié)果總位數(shù) 
    
    int fact[3000] = {0};
    fact[0] = 1;
    
    if( N>=0)
    {
        if( N==0 || N==1)   
        {
            temp = 1;
            printf("%d", temp);
        }
        else 
        {
            for(int i=2; i<=N; i++)
            {
                for( int j=0; j<k; j++)
                {
                    temp = i * fact[j] + m;
                    fact[j] = temp %10 ; 
                    m = temp / 10 ;
                    
                    if( m && j==k-1)// 當(dāng)有進(jìn)位且已經(jīng)處理到最前位時(shí)才開(kāi)拓目標(biāo)數(shù)組的下一位 
                        k++; 
                }
            }
                
            for(int i=k-1; i>=0; i--)   
                printf("%d", fact[i]);
        }
    }
    else printf("Invalid input");
}

轉(zhuǎn)載請(qǐng)注明出處:github.com/MuziJin

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容