微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐