HDU 1042 N!(高精度计算阶乘)

来源:互联网 发布:巧克力哪一款好吃知乎 编辑:程序博客网 时间:2024/05/17 04:39

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
123
Sample Output
126

题意:

就是求n的阶乘。

思路:

开一个数组模拟乘法运算即可。

#include<iostream>#include<cstdio>#include<string>#include<cmath>#include<cstring>using namespace std;const int mx = 41000;int a[mx];int main (){int n;while(scanf("%d",&n) != EOF){memset(a, 0, sizeof(a));a[0] = a[1]= 1;for(int i=2 ;i <= n; i++){int c = 0;for(int j = 1; j < mx; j++){int te = a[j] * i + c;a[j] = te % 10;c = te / 10;}}int tt;for(int i = mx-1; i>0; i--)if(a[i] != 0) {tt = i;break;} for(int i = tt; i > 0;i--)printf("%d",a[i]);puts("");}}


原创粉丝点击