高精度乘法

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. }

 

posted on 2014-04-18 22:35  左手代码右手诗  阅读(538)  评论(0编辑  收藏  举报