习题 92:高精度算阶乘★★

该博客介绍了如何精确计算1到10000之间的整数阶乘,提供多组测试数据,并展示了例如10!和100!的计算结果。100!因结果过长,被拆分为三行显示,每行60字符。问题难度被标记为Easy。
摘要由CSDN通过智能技术生成

 /*题目描述:
算出n!的完整结果,其中n!=1*2*3*...*n

输入:
多组测试数据,一行一组,每行仅一个数n
其中0<=n<=10000

输出:
输出相应的n!的结果,必须精确到个位

样例输入:
10
20
100

样例输出:
3628800
2432902008176640000
933262154439441526816992388562667004907159682643816214685929
638952175999932299156089414639761565182862536979208272237582
51185210916864000000000000000000000000

其它信息:
最后一个100!的结果由于过长,故拆分成三行,每行60字符,请见谅

难度:Easy

*/

#include <iostream>
#include <iomanip>
#define MAX_SIZE 10000
using namespace std;
//end is the last updated bit
int bigMulti(int *result, int n,int size, int end)
{
    int i,j, carry, tmp;
    carry = 0;
    for(j = 1;j <= n; j++)
    {
        for(i = MAX_SIZE; --i >=end;)
        {
            tmp = result[i] * j + carry;
            result[i] = tmp % 10000;
            carry = tmp / 10000;
        }
        while(carry)
        {
            result[--end] = carry % 10000;
            carry /= 10000;
        }
    }
    return end;
}
int main(void)
{
    int n, end;
    int result[MAX_SIZE];//如果这里声明为char会导致速度减慢
    while(EOF!=scanf("%d",&n))
    {
        memset(result,0,MAX_SIZE*sizeof(int));
        result[MAX_SIZE - 1] = 1;// set result to 1
        end = MAX_SIZE - 1;
        end = bigMulti(result, n,MAX_SIZE, end);
        cout << result[end++];
        for(; end < MAX_SIZE;)
        {
            printf("%04d",result[end++]);
        }
        cout << endl;
        //result[j] = 0;
        //cout << result << endl;
    }
    return 0;
}
/*result:

  
  
78737

Name: " younthu" Problem ID " 92"
Submit Time: 2008/9/28-13:35

G++: Compile OK

Test  1:    Accepted    Time = 0 ms
Test  2:    Accepted    Time = 0 ms
Test  3:    Accepted    Time = 267 ms
--------------------------------
Problem ID     92
Test Result    Accepted
Total Time     267 ms
Total Memory   228 Kb / 2000 Kb
Code Length    817 Bytes
*/
<script>sh_highlightElement(document,code5);</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值