首页 > 其他 > 详细

杭电 1042 N!

时间:2014-04-14 02:34:29      阅读:406      评论:0      收藏:0      [点我收藏+]

N!

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) :    Accepted Submission(s) :

Problem Description

Given an integer N(0 ≤ ≤ 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

1
2
3

 

 

Sample Output

1
2
6


 

 

 

 

 

 

     大数的阶乘,用到数组,一组组储存数据,然后统一输出,用循环一个数一个数乘就不会导致空间不足,下面代码是没5位一进位的,代码不复杂,认真看懂吧!

 

 

 

 

代码如下:

#include<stdio.h>

int main()

{

    int a[100001],b,n,m,i,j;

    while(~scanf("%d",&n))

    {

        m=0;a[0]=1;

        for(i=1;i<=n;i++)

        {

            b=0;

            for(j=0;j<=m;j++)

            {        

                a[j]=a[j]*i+b;

                b=a[j]/100000;

                a[j]=a[j]%100000;

            }//数组存数

            if(b>0)

            {

                m++;

                a[m]=b;

            }

        }

        printf("%d",a[m]);//将最高位的几位数分开输出,避开前置补0.

        for(i=m-1;i>=0;i--)

            printf("%05d",a[i]);//每5位一输出

        printf("\n");

    }    

    return 0;    

}

杭电 1042 N!,布布扣,bubuko.com

杭电 1042 N!

原文:http://blog.csdn.net/hanhai768/article/details/23622403

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!