n! 高精度算法(大数*小数)

N!  HDU - 1042

https://cn.vjudge.net/problem/HDU-1042
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

代码:

#include <bits/stdc++.h>
#define maxn 200000
using namespace std;
int f[maxn],len;
int main(){
	int n;
	while(~scanf("%d", &n)){
		memset(f,0,maxn);
		f[0]=1;
        int ans=1;
		for(int i=2;i<=n;i++){
			int c=0;
			for(int j=0;j<ans;j++){
				int medium=f[j]*i+c;
				f[j]=medium%10;
				c=medium/10;
			}
            while(c){
            	f[ans]=c%10;
            	c=c/10;
            	ans++;
       		}
		}
		len=maxn;
		while(len--)
			if(f[len]) break;
		len++;
		while(len--) printf("%d", f[len]);
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44410512/article/details/88086956