- #define SIGN(A) ((A > 0) ? 1 : -1)
- int IntegerMultiply(int X, int Y, int N)
- {
- int sign = SIGN(X) * SIGN(Y);
- int x = abs(X);
- int y = abs(Y);
- if((0 == x) || (0 == y))
- return 0;
- if (1 == N)
- return x*y;
- else
- {
- int XL = x / (int)pow(10., (int)N/2);
- int XR = x – XL * (int)pow(10., N/2);
- int YL = y / (int)pow(10., (int)N/2);
- int YR = y – YL * (int)pow(10., N/2);
- int XLYL = IntegerMultiply(XL, YL, N/2);
- int XRYR = IntegerMultiply(XR, YR, N/2);
- int XLYRXRYL = IntegerMultiply(XL – XR, YR – YL, N/2) + XLYL + XRYR;
- return sign * (XLYL * (int)pow(10., N) + XLYRXRYL * (int)pow(10., N/2) + XRYR);
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int x = 1234;
- int y = 4321;
- cout<<“x * y = “<<IntegerMultiply(x, y, 4)<<endl;
- cout<<“x * y = “<<x*y<<endl;
- return 0;
- }
分治法解决大整数乘法
原文作者:大整数乘法问题
原文地址: https://blog.csdn.net/wyh_rejesh/article/details/51365983
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/wyh_rejesh/article/details/51365983
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。