Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782156
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-08-23 08:42:54

8.5
Recursive Multiply: Write a recursive function to multiply two positive integers without using
the * operator (or / operator). You can use addition, subtraction, and bit shifting, but you should
minimize the number of those operations

这道题很有意思,我自己的实现因为参考了前面scheme /guile 实现,所以很简单。

点击(此处)折叠或打开

  1. //my solution
  2.     public static int multiply(int x,int y){
  3.         if(x==0){
  4.             return 0;
  5.         }
  6.         return y+multiply(x-1, y);
  7.     }
下面是除法的实现,当然想想其实可以多写一个 x==y . 可能会更容易明白,但是意义不大。
limitation: 这个除法和乘法其实都只能计算正数,不能计算负数的。

点击(此处)折叠或打开

  1. /* Divide compute a/b with recursion */
  2.     public static int divide(int x, int y){
  3.         if(x < y){
  4.             return 0;
  5.         }else{
  6.             return 1+divide(x-y, y);
  7.         }
  8.     }

点击(此处)折叠或打开

  1. /* Divide compute a/b with recursion */
  2.     public static int divide(int x, int y){
  3.         if(x < y){
  4.             return 0;
  5.         }else if(x == y){
  6.             return 1;    
  7.         }else{
  8.             return 1+divide(x-y, y);
  9.         }
  10.     }
官方的solution 如下,有3个,我把他们写在了一块。

点击(此处)折叠或打开

  1. //solution 1
  2.     static int minProduct(int a, int b){
  3.         int bigger = a< b ? b :a;
  4.         int smaller = a<b ? a :b;
  5.         return minProductHelper(smaller, bigger);
  6.     }
  7.     
  8.     static int minProductHelper(int smaller, int bigger){
  9.         if(smaller == 0){
  10.             // 0 x bigger = 0
  11.             return 0;
  12.         }else if(smaller == 1){
  13.             // 1 x bigger = bigger
  14.             return bigger;
  15.         }
  16.         
  17.         //compute half, if uneven , compute other half, if even double it
  18.         int s = smaller >> 1;    //Divide by 2
  19.         int side1 = minProduct(s, bigger);
  20.         int side2 = side1;
  21.         if(smaller % 2 ==1){
  22.             side2 = minProductHelper(smaller-s, bigger);
  23.         }
  24.         return side1 + side2;
  25.     }
  26.     
  27.     //solution 2
  28.     static int minProduct2(int a, int b){
  29.         int bigger = a > b ? a : b;
  30.         int smaller = a < b ? a : b;
  31.         int memo[] = new int[smaller+1];
  32.         return minProduct2(smaller,bigger, memo);
  33.     }
  34.     
  35.     static int minProduct2(int smaller, int bigger, int[] memo){
  36.         if(smaller == 0){
  37.             return 0;
  38.         }else if (smaller == 1){
  39.             return bigger;
  40.         }else if(memo[smaller] > 0){
  41.             return memo[smaller];
  42.         }
  43.         //compute half, if uneven, compute other half,if even, double it
  44.         int s = smaller >> 1;        //Divide by 2
  45.         int side1 = minProduct2(s, bigger,memo);        //compute half
  46.         int side2 = side1;
  47.         if(smaller % 2 == 1){
  48.             side2 = minProduct2(smaller -s, bigger, memo);
  49.         }
  50.         
  51.         //sum and cache
  52.         memo[smaller] = side1+side2;
  53.         return memo[smaller];
  54.     }
  55.     
  56.     //solution 3
  57.     static int minProduct3(int a ,int b){
  58.         int bigger = a < b ? b : a;
  59.         int smaller = a < b ? a : b;
  60.         
  61.         return minProductHelper3(smaller, bigger);
  62.     }
  63.     
  64.     static int minProductHelper3(int smaller, int bigger){
  65.         if(smaller == 0){
  66.             return 0;
  67.         }
  68.         if(smaller == 1){
  69.             return bigger;
  70.         }
  71.         int s= smaller >> 1;        //Divide by 2
  72.         int halfProd = minProduct3(s, bigger);
  73.         
  74.         if(smaller % 2 == 0){
  75.             return halfProd + halfProd;
  76.         }else{
  77.             return halfProd + halfProd + bigger;
  78.         }
  79.     }
  80.     
最下面是测试代码


点击(此处)折叠或打开

  1. public static void main(String[] args){
  2.         int x = 20;
  3.         int y = 9;
  4.         int a = 18;
  5.         int b = -2;
  6.         int c = -1;
  7.         
  8.         int answer = multiply(x,y);
  9.         System.out.println("answer is "+answer);
  10.         int answer3 = minProduct3(x,y);
  11.         System.out.println("answer3 is "+answer3);
  12.         int answer2 = minProduct2(x,y);
  13.         System.out.println("answer2 is "+answer2);
  14.         int answer1 = minProduct(x,y);
  15.         System.out.println("answer1 is "+answer1);
  16.         
  17.         /* compute x / y
  18.          *
  19.          */
  20.         int quotient1 = divide(x,y);
  21.         int quotient2 = divide(a, y);
  22.         int quotient3 = divide(b, c);
  23.         System.out.println("quotient1 is "+quotient1);
  24.         System.out.println("quotient2 is "+quotient2);
  25.         System.out.println("quotient3 is "+quotient3);
  26.     }

负数相除会得到0,因为-2 < -1 .

阅读(1079) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~