高精度乘法

You'll be given two intergers. The number of the digits of each is from 1 to 200, inclusive.

    And what you need to do is to culcalate the product of the two intergers.

    The number of the digits of the product won't be more than 400.



Input format:

    Two lines in total.

    The first line is an interger a(0 <= a < 10200), and the second line is an interger b(0 <= b < 10200).

Output format:

    Only one line, which is the product of a and b.



Sample input:

12

2

Sample output:

24

 

 

 

  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int multiplication(char*a,char*b,int*c);
  5. int main(){
  6. int c[400+10], i;
  7. int k;
  8. char a[200+10]={0};
  9. char b[200+10]={0};
  10. memset(c,0,sizeof(c));
  11. gets(a);
  12. gets(b);
  13. if((a[0]=='0')||(b[0]=='0')){
  14. printf("0\n");
  15. }else{
  16. k = multiplication(a, b, c);
  17. for(i = k; i >=1; i--) printf("%d", c[i]);
  18. printf("\n");
  19. }
  20. return0;
  21. }
  22. int multiplication(char*a,char*b,int*c){
  23. int i, j, m, tem, k =405, la, lb;
  24. la = strlen(a);
  25. lb = strlen(b);
  26. for(i = lb -1; i >=0; i--){
  27. for(j = la -1, m =0; j >=0; j--){
  28. tem = c[(lb-i)+(la-j)-1];
  29. c[(lb-i)+(la-j)-1]=
  30. ((a[j]-'0')*(b[i]-'0')+ m + c[(lb - i)+(la - j)-1])%10;
  31. m =((a[j]-'0')*(b[i]-'0')+ m + tem)/10;
  32. }
  33. c[(lb-i)+(la-j)-1]= m;
  34. }
  35. while(!c[k]) k--;
  36. return k;
  37. }

 

转载于:https://www.cnblogs.com/sysu-zhengwsh/p/3674201.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值