HDU1042 N!

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 80556    Accepted Submission(s): 23614


Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
 

Input
One N in one line, process to the end of file.
 

Output
For each N, output N! in one line.
 

Sample Input
1 2 3
 

Sample Output
1 2 6

     模擬大數乘法運算,直接上模板,這樣比較費時,4位一組4位一組的運算會節省很多時間並且用一個變量來表示大數的長度

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int num[100002],n;
void multiply(int k){
    int f = 0;
    for(int i = 0;i < 50000;i++){
        num[i] = num[i]*k+f;
        f = num[i]/10;
        num[i]%=10;
    }
}
int main()
{
    while(~scanf("%d",&n)){
        memset(num,0,sizeof(num));
        num[0] = 1;
        for(int i = 1;i <= n;i++)
            multiply(i);
        int j;
        for(j = 50000;j>=0;j--){
            if(num[j]!=0)
                break;
        }
        for(;j>=0;j--)
            printf("%d",num[j]);
        printf("\n");
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章