大数乘法

#include <iostream>

#include <string>

#include <memory.h>

using namespace std;

int m[500];

bool mf[500]; 

void multiply(string num1, string num2)

{

int i = 0;

int j = 0;

int num1l = num1.length();

int num2l = num2.length();

int temp;

int ml;

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

{

for(j = num1l - 1; j >= 0; j--)

{

mf[num1l + num2l -2 - i - j] = 1;

m[num1l + num2l -2 - i - j] += (num1[j] - '0') * (num2[i] - '0');

}

}

ml = 0;

while(mf[ml])

{

temp = m[ml]/10;

m[ml] %= 10;

if(temp)

{

m[ml + 1] += temp;

mf[ml + 1] = true;

}

ml++;

}

for(i = ml - 1; i >= 0; i--)cout<<m[i];

cout<<endl;

}

int main()

{

string num1, num2;

cin>>num1;

cin>>num2;

memset(m, 0, sizeof(int) * 500);

memset(mf, 0, sizeof(bool) * 500);

multiply(num1, num2);

return 0;

}

最大积的位数可以达到500位,可行性有待考证 


转载于:https://my.oschina.net/u/1392946/blog/511632

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值