大數乘法,階乘計算,計算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
****************************************************************/