Fork me on GitHub

题目1076:N的阶乘(大数乘法)

题目链接:http://ac.jobdu.com/problem.php?pid=1076

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAX_SIZE 10010
using namespace std;
 
int n;
int pos[MAX_SIZE];
 
int main(){
    while(~scanf("%d",&n)){
        memset(pos,0,sizeof(pos));
        if(0==n){
            printf("1\n");
            continue;
        }
        int i,j;
        int length = 1;
        pos[0]=1;
        for(i = 1 ; i <= n ; i++){
            int carry = 0;
            for(j = 0 ; j < length ; j++){
                pos[j] = pos[j] * i + carry;
                if(pos[j]>=10){
                    carry = pos[j]/10;
                    pos[j] = pos[j]%10;
                }
                else{
                    carry = 0;
                }
            }
            while(carry!=0){
                pos[length++] = carry % 10;
                carry/=10;
            }
        }
        for(i = MAX_SIZE ; i >= 0 ; i--){
            if(pos[i]!=0)
                break;
        }
        for(j = i ; j >= 0 ; j--){
            printf("%d",pos[j]);
        }
        printf("\n");
    }
    return 0;
}
/**************************************************************
    Problem: 1076
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:1480 ms
    Memory:1560 kb
****************************************************************/

 

posted @ 2017-04-16 15:34  伊甸一点  阅读(290)  评论(0编辑  收藏  举报