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,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;
}

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...