Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include<cstdio>
  2. #include<cmath>
  3.  
  4. using namespace std;
  5.  
  6. #define SIGN(A) ((A > 0) ? 1 : -1)
  7. int divideConquer(int X, int Y, int n){
  8. int sign = SIGN(X) * SIGN(Y);
  9. int x = abs(X);
  10. int y = abs(Y);
  11. if(x == 0 || y == 0){
  12. return 0;
  13. }else if(n == 1){
  14. return sign * x * y;
  15. }else{
  16. int A = (int) x / pow(10, (int)(n / 2));
  17. int B = x - A * pow(10, n / 2);
  18. int C = (int) y / pow(10, (int)(n / 2));
  19. int D = y - C * pow(10, n / 2);
  20. int AC = divideConquer(A, C, n / 2);
  21. int BD = divideConquer(B, D, n / 2);
  22. int ABDC = divideConquer((A - B), (D - C), n / 2) + AC + BD;
  23. return sign * (AC * pow(10 , n) + ABDC * pow(10, (int)(n / 2)) + BD);
  24. }
  25. }
  26.  
  27. int main(){
  28. int x, y, n;
  29. scanf("%d%d%d", &x, &y, &n);
  30. printf("x 和 y的乘积为:%d", divideConquer(x, y, n));
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement