1.将整数转换为数组
如123456转换成 {0,0,0,0,1,2,3,4,5,6},数组长度根据数据规模自己决定
void convertArr(int* arr,int len,long long l) { int i=0; int num; while(l!=0){ num=l%10; l-=num;l/=10; arr[len-i-1]=num; i++; } }
2.按乘法规则将数组元素按位相乘
void multy(int* arr1,int* arr2,int* arr3,int len) { int p1=0; int p2=0; while(arr1[p1]==0) p1++; while(arr2[p2]==0) p2++; int index3=0; for(int i=len-1;i>=p2;i--){ for(int j=len-1;j>=p1;j--){ int num=arr1[j]*arr2[i]+arr3[j-index3]; if(num<10) arr3[j-index3]=num; else{ arr3[j-index3]=num%10; arr3[j-index3-1]+=num/10; } } index3++; } }
3.迭代乘法
int* Factorial ( const int N,int arr1[],int len) { int arr2[len]; int arr3[len]; for(int i=0; i<len; i++) { arr2[i]=0; arr3[i]=0; } convertArr(arr1,len,1); for(int k=2; k<=N; k++) { //arr2=k; convertArr(arr2,len,k); //arr3=arr1*arr2 multy(arr1,arr2,arr3,len); //arr1=arr3 for(int i=0; i<len; i++) { arr1[i]=arr3[i]; arr3[i]=0; } } return arr1; }
4.main函数测试
int main() { int len=9000; int arr1[len]; for(int i=0; i<len; i++) { arr1[i]=0; } Factorial(5,arr1,len); int p1=0; while(arr1[p1]==0) p1++; for(int i=p1; i<len; i++) { printf("%d",arr1[i]); } return 0; }