大数的阶乘算法

本文提供了一个计算n的阶乘的C++程序示例。该程序通过迭代方式逐步计算出给定整数n的阶乘,并能处理较大数值的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原帖及讨论:http://bbs.bccn.net/thread-179854-1-1.html

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国  http://www.bccn.net
*/ 作者: xmzjerry    E-mail:xmzjerry@163.com    
*/ 时间: 2007-10-22  编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------


 

#include "stdafx.h"
#include "stdio.h"
#include "iostream.h"

int main(int argc, char* argv[])
{
    int carry,n,j;
    int a[2000];
    int digit=1;
    int temp,i;
    cout<<"please enter n:"<<endl;
    cin>>n;
    a[0]=1;
    for(i=2; i<=n; i++)
    {
        for(carry=0,j=1; j<=digit; ++j)
        {
            temp=a[j-1]*i+carry;
            a[j-1]=temp%10;
            carry=temp/10;
        }
        while(carry)
        {
            //digit++;
            a[++digit-1]=carry%10;
            carry/=10;
        }
    }
    cout<<"the result is:"<<endl;
    for(int k=digit; k>=1; --k)
        cout<<a[k-1];
    cout<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值