大整数乘法c语言程序,大数相乘算法

[c]代码库/**************************************

大数相乘算法

算法复杂度为:O(longhta*longthb)

longtha为乘数的位数

longhtb为被乘数的位数

***************************************/

#include 415

#include

#include

#define LEN 1000

void mult(char [], char [], char []);

main()

{

char op1[LEN], op2[LEN], op3[LEN * 2 - 1];

scanf("%s%s", op1, op2);

mult(op1, op2, op3);

printf("%s\n", op3);

getch();

return 0;

}

void reverse(char a[])

{

int longth = strlen(a);

int i;

for(i = 0; i < longth / 2; i++)

{

char t;

t = a[i];

a[i] = a[longth - i - 1];

a[longth - i - 1] = t;

}

}

void mult(char op1[LEN], char op2[LEN], char ans[LEN * 2 - 1])

{

char top1[LEN];

char top2[LEN];

strcpy(top1, op1);

strcpy(top2, op2);

reverse(top1);

reverse(top2);

int k;

int top1s = strlen(top1);

int top2s = strlen(top2);

for(k = 0; k < top1s + top2s; k++)

{

ans[k] = '0';

}

int i, j;

int jw, ys;

int longth;

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

{

jw = 0;

for(i = 0; i < top1s; i++)

{

ys = ((top1[i] - '0') * (top2[j] - '0') + jw + ans[i + j] - '0') % 10;

jw = ((top1[i] - '0') * (top2[j] - '0') + jw + ans[i + j] - '0') / 10;

ans[i + j] = ys + '0';

}

if(jw > 0)

{

ans[i + j] = jw + '0';

}

}

longth = i + j - 1;

if(jw > 0)

ans[longth++] = jw + '0';

ans[longth] = '\0';

reverse(ans);

}

[代码运行效果截图]

ed2f3687e2bb6dbdb2a9aa1525dde1dd.png

694748ed64b9390909c0d88230893790.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值