Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
如n=10000,求N!
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
如n=10000,求N!
采用万进制,逢万进一,结果存于a数组,结果倒序输出,每个a[i]不足四位前面补0
#include <bits/stdc++.h>
using namespace std;
void f(int n){
int a[10000+1]={1},carry,digits=1;
for(int i=2;i<=n;i++){
carry=0;
for(int j=0;j<digits;j++){
a[j]=a[j]*i+carry;
carry=a[j]/10000; //进位
a[j]=a[j]%10000; //余数
}
if(carry>0)
a[digits++]=carry;
}
printf("%d",a[digits-1]);
for(int j=digits-2;j>=0;j--)
printf("%04d",a[j]);
printf("\n");
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
f(n);
}
return 0;
}