实现整数的乘法、减法和除法运算,只允许使用加号

/**

 * 功能:实现整数的乘法、减法和除法运算。只允许使用加号。

 */

 

[java] view plain copy

 

  1. //减法  
  2.     public static int minus(int a,int b){  
  3.         return a+negate(b);  
  4.     }  
  5.       
  6.     //取反  
  7.     /** 
  8.      * 思路:对正数k的取反,只需要将-1连续加k次;对负数k的取反,只需要将1连续加k次。 
  9.      * @param a 
  10.      * @return 
  11.      */  
  12.     public static int negate(int a){  
  13.         int neg=0;  
  14.         int d=a>0?-1:1;  
  15.         while(a!=0){  
  16.             neg+=d;  
  17.             a+=d;             
  18.         }  
  19.         return neg;  
  20.     }  
  21.       
  22.     //乘法  
  23.     /** 
  24.      * 思路:a乘以b,即为a连续加b次。 
  25.      * @param a 
  26.      * @param b 
  27.      * @return 
  28.      */  
  29.     public static int multiply(int a,int b){  
  30.         if(a<b)  
  31.             return multiply(b,a);  
  32.           
  33.         int sum=0;        
  34.         for(int i=abs(b);i>0;i--){  
  35.             sum+=a;  
  36.         }  
  37.           
  38.         if(b<0)  
  39.             sum=negate(sum);  
  40.           
  41.         return sum;  
  42.     }  
  43.       
  44.     //取绝对值  
  45.     /** 
  46.      * 思路:即对负数取反。 
  47.      * @param a 
  48.      * @return 
  49.      */  
  50.     public static int abs(int a){  
  51.         if(a<0)  
  52.             return negate(a);  
  53.         else   
  54.             return a;  
  55.     }  
  56.       
  57.     //除法  
  58.     /** 
  59.      * 思路:利用等式a=xb,将b与其自身连加直至得到a,就能算出x的值。x的值为b连加的次数。 
  60.      * 注意:若a不能被b整除,对于整数除法,即对结果向下取舍。 
  61.      * @param a 
  62.      * @param b 
  63.      * @return 
  64.      * @throws java.lang.ArithmeticException 
  65.      */  
  66.     public static int divide(int a,int b) throws java.lang.ArithmeticException{  
  67.         if(b==0){  
  68.             throw new java.lang.ArithmeticException("Error");  
  69.         }  
  70.           
  71.         int absa=abs(a);  
  72.         int absb=abs(b);  
  73.           
  74.         int product=0;  
  75.         int x=0;  
  76.         while(product+absb<=absa){  
  77.             product+=absb;  
  78.             x++;  
  79.         }  
  80.           
  81.         if(a<0&&b<0||a>0&&b>0)  
  82.             return x;  
  83.         else  
  84.             return negate(x);  
  85.     }

或者:

解题思路:题目要求只允许使用加号,我想意思指的是不能直接使用乘、减、除等运算符,而比较运算符等其他运算符还是可以使用的。

1. a*b:将问题转换成|b|个a相加,或者|a|个b相加,最后根据a、b的符号确定返回值的符号。

2.  a-b:转化成a+[-b]补,而[b]补与[-b]补之间的转换关系:连同符号位一起按位取反,再加1。

3. a/b: 问题转化成b*x = a,情形与1类似。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

public int calc(int a, int b, int type) {

    if (!(type == 1 || type == 0 || type == -1))

        throw new RuntimeException("Invalid Input");

    switch (type) {

    case 1:

        return multiply(a, b);

    case 0:

        return divide(a, b);

    default:

        return minus(a, b);

    }

}

 

public int multiply(int a, int b) {

    if(b==0)// 与0相乘,返回0

        return 0;

    // 将问题转换成b个a相加

    if(Math.abs(a)<Math.abs(b)){// b取绝对值的较小者,可以加速运算

        int tmp = a;

        a = b;

        b = tmp;

    }

    int sum = 0;

    for (int i = 1; i <= Math.abs(b); i++) {

        sum+=a;

    }

    // 确定返回值的符号    

    return b>0?sum:(~sum)+1;

}

 

public int minus(int a, int b) {

    // a-b转化成a+[-b]补,而[b]补与[-b]补之间的转换关系:连同符号位一起按位取反,再加1。

    int rb = (~b)+1;

    return a+rb;

}

 

public int divide(int a, int b) {

    if(b==0)

        throw new RuntimeException("b cannot be 0");

    // 问题转化成b*x = a

    int sum = 0,count = 0;

    while(sum<Math.abs(a)){

        sum += Math.abs(b);

        count++;

    }

    sum = sum>Math.abs(a)?--count:count;

    // 若a、b同号则返回正数

    return ((a>>31)^(b>>31))==0?count:(~count)+1;

}

转载于:https://my.oschina.net/u/2822116/blog/790685

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值