145 不用算术运算符实现整数的加减乘除运算
Table of Contents

ac

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static long mod = (long)Math.pow(2,29);

    public static void main(String[] args) throws Exception {
        String str = reader.readLine();
        String[] strArr = str.split(" ");
        int a = Integer.valueOf(strArr[0]);
        String operator = strArr[1];
        int b = Integer.valueOf(strArr[2]);
        System.out.println(operatorVal(a, b, operator));
    }

    static int operatorVal(int a, int b, String operator){
        if(operator.equals("+")){
            return add(a, b);
        }
        if(operator.equals("-")){
            return sub(a, b);
        }
        if(operator.equals("*")){
            return multi(a, b);
        }
        if(operator.equals("/") || operator.equals("\\")){
            return divide(a, b);
        }
        return -1;
    }

    static int negative(int a) {
        return ~(a-1); // 最大负整数问题 -a = ~(a-1)
    }

    static int add(int a, int b){
        if(b == 0){
            return a;
        }
        int xor = a ^ b; // 异或是不带进位的加
        int carry = (a & b) << 1; // 进位和
        return add(xor, carry);
    }

    static int sub(int a, int b){
        if(b == 0){
            return a;
        }
        return add(a, negative(b));
    }

    static int multi(int a, int b){
        if(a == 0 || b == 0){
            return 0;
        }
        int sign1 = (a >> 31) & 1;
        int sign2 = (b >> 31) & 1;
        if(sign1 == 0 && sign2 == 0){
            return bit_multi(a, b);
        }else if(sign1 == 0 && sign2 == 1){
            return negative(bit_multi(a, negative(b)));
        }else if(sign1 == 1 && sign2 == 0){
            return negative(bit_multi(b, negative(a)));
        }else {
            return bit_multi(negative(a), negative(b));
        }
    }

    static int bit_multi(int a, int b){
        if(b == 0){
            return 0;
        }
        int ans = 0;
        while (b != 0){
            if((b & 1) == 1){
                ans = add(ans, a); // 第一次加 a,第二次加 a^1,第三次加 a^4
            }
            a = a << 1; // 左移,相当于乘以2
            b = b >> 1; // 右移,为得到接着位,有种快速幂的感觉
        }
        return ans;
    }

    // 类似a包含多少个b
    static int divide(int a, int b){
        if(a == 0){
            return 0;
        }
        int sign1 = (a >> 31) & 1;
        int sign2 = (b >> 31) & 1;
        if(sign1 == 0 && sign2 == 0){
            return divide_positive(a, b);
        }else if(sign1 == 0 && sign2 == 1){
            return negative(divide_positive(a, negative(b)));
        }else if(sign1 == 1 && sign2 == 0){
            return negative(divide_positive(negative(a), b));
        }else {
            return divide_positive(negative(a), negative(b));
        }
    }

    // 类似a包含多少个b
    static int divide_positive(int a, int b){
        int count = 0;
        while (a >= b){
            a = sub(a, b);
            count = add(count, 1);
        }
        return count;
    }

}
/*
5 + 7
5   0101
7   0111
12  1100

5 ^ 7       =  0010
5 & 7 << 1  =  1010


232977618 \ -59857512
-3
*/