0
点赞
收藏
分享

微信扫一扫

HDU 1042(大数)


                       N!
   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.


   Sample Input
   1
   2
   3


   Sample Output
   1
   2

   6

//由于大数求阶乘利用数组模拟乘法运算,所以必须从低位开始相乘
#include <stdio.h>
#include <string.h>
int a[50000];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        a[0]=1;
        int p=1; //p为阶乘所到位数的下一个所以输出时不需要判断0
        for(int i=2;i<=n;i++)
        {
            int c=0,j; //c为相乘溢出的值
            for(j=0;j<p;j++)
            {
                int s=a[j]*i+c;
                a[j]=s%10;
                c=s/10;
            }
            while(c)
            {
                a[j++]=c%10;
                c/=10;
            }
            p=j;
        }
        for(int j=p-1;j>=0;j--)
            printf("%d",a[j]);
        printf("\n");
    }
    return 0;
}



举报

相关推荐

0 条评论