HDU 1042 N!

Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 

InputOne N in one line, process to the end of file. 
OutputFor each N, output N! in one line. 
Sample Input

1
2
3

Sample Output

1
2
6

  

高精度问题:大整数乘法的应用

其核心思想就是把计算结果每一位上的数字保存到一个数组成员中,例如:
把124保存至数组中,保存结果应该是result[0] =4;result[1] =2;result[2] =1
把整个数组看成一个数字,这个数字和一个数相乘的时候,需要每一位都和这个乘数进行相乘运算还需要把前一位的进位加上。

写法如下:int 结果 = result[x] * 乘数 + 进位;
每一位的计算结果有了,把这个结果的个位数拿出来放到这个数组元素上:result[x] = 结果%10;
接下来的工作就是计算出进位:进位 = 结果 / 10;
这样一位一位的把整个数组计算一遍,最后可能还有进位,用同样的方法,把进位的数值拆成单个数字,放到相应的数组元素中。最后从后往前输出结果。

#include<bits/stdc++.h>
#define MAX 100000
using namespace std;
int main()
{
    int n,a[MAX];
    int i,j,k,count,temp;
    while(cin>>n)
    {
        a[0]=1;
        count=1;
        for(i=1;i<=n;i++)
        {
            k=0;
            for(j=0;j<count;j++)
            {
                temp=a[j]*i+k;
                a[j]=temp%10;
                k=temp/10;    
            }
            while(k)//记录进位    
             {
                a[count++]=k%10;
                k/=10;
            }
        }
        for(j=MAX-1;j>=0;j--)      
            if(a[j])
                break;//忽略前导0
            for(i=count-1;i>=0;i--)
                cout<<a[i];
            cout<<endl;
    }
    return 0;
}