hdu 1042 高精乘低精 高精度算法
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
题意:求n的阶乘,只不过n的数比较大:0<n<=10000。这是一个高精度的算法,高精乘低精。计算了下10000的阶乘有35660位。。。。
思路:模拟乘法计算。用数组存结果,再从数组个位取每一位数乘下一位阶乘。用代码实现。代码如下:
#include<stdio.h> #include<string.h> int len; int ans[60000]; void multiply(int i) { int res=0,t; for(int j=0;j<len;j++) { t=ans[j]*i+res;//res是进位 ans[j]=t%10; res=t/10; } while(res)//最后把最后的一位乘下来的进位加到后面 { ans[len++]=res%10; res/=10; } } int main() { int n; while(~scanf("%d",&n)) { len=1; memset(ans,0,sizeof(ans)); ans[0]=1;//初始为1 for(int i=2;i<=n;i++) multiply(i); for(int i=len-1;i>=0;i--) printf("%d",ans[i]); printf("\n"); } }