hdu1042 N!_大数

来源:互联网 发布:linux 虚拟化 多台 编辑:程序博客网 时间:2024/06/07 13:28

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 80509    Accepted Submission(s): 23599


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
123
 

Sample Output
126

/*
 * 高精度问题:大整数乘法的应用
*其核心思想就是把计算结果每一位上的数字保存到一个数组成员中,例如:
*把124保存至数组中,保存结果应该是result[0] =4;result[1] =2;result[2] =1
*把整个数组看成一个数字,这个数字和一个数相乘的时候,需要每一位都和这个乘数进行相乘运算还需要把前一位的进位加上。
*写法如下:int 结果 = result[x] * 乘数 + 进位;
*每一位的计算结果有了,把这个结果的个位数拿出来放到这个数组元素上:result[x] = 结果%10;
*接下来的工作就是计算出进位:进位 = 结果 / 10;
*这样一位一位的把整个数组计算一遍,最后可能还有进位,用同样的方法,把进位的数值拆成单个数字,放到相应的数组元素中。最后从后往前输出结果。
 */

//// Created by Admin on 2017/3/25.//#include <cstdio>#define MAX 100010int ans[MAX];int main(){    int n;    while(~scanf("%d",&n)){        ans[0]=1;        int count=1,k;        for (int i = 1; i <= n; ++i) {            k=0;            for (int j = 0; j < count; ++j) {                int temp=ans[j]*i+k;                ans[j]=temp%10;                k=temp/10;            }            while (k){                ans[count++]=k%10;                k/=10;            }        }        for (count--; count >= 0 ; count--)            printf("%d",ans[count]);        printf("\n");    }    return 0;}


0 0