大数相乘 算阶乘

本文介绍了一种处理大数乘法的算法实现,通过字符串操作而非传统的整型运算,能够处理超出常规数据类型限制的大数值乘法。该算法使用C++编写,详细展示了如何逐位相乘并进位,最终合成结果。

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

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <string>
#include <vector>
#include <deque>
#include <sstream>
using namespace std;
string BigNumMultiply(string str1,string str2)
{
    int size1=str1.size(),size2=str2.size();
    string str(size1+size2,'0');
    for(int i=size2-1;i>=0;--i)
    {
        int mulflag=0,addflag=0;
        for(int j=size1-1;j>=0;--j)
        {
            int temp1=(str2[i]-'0')*(str1[j]-'0')+mulflag;
            mulflag=temp1/10;
            temp1=temp1%10;
            int temp2=str[i+j+1]-'0'+temp1+addflag;
            str[i+j+1]=temp2%10+48;
            addflag=temp2/10;
        }
        str[i]+=mulflag+addflag;
    }
  if(str[0]=='0')
  str=str.substr(1,str.size());
  return str;
}
int main()
{
  int n;
  string f[1000],str1;
  cin>>n;
  f[1]='1',f[2]='2';
  for(int i=3;i<1000;i++)
  {
    str1=to_string(i);
    f[i]=BigNumMultiply(f[i-1],str1);
  }
  cout<<f[n]<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值