[左神面试指南] 位运算篇

CD142 不用额外变量交换两个整数的值

/* 模拟 */
public class CD142_1
{
    public static void solution(int a, int b)
    {
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println(a + " " + b);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int a, b;
        a = in.nextInt();
        b = in.nextInt();
        solution(a, b);

    }
}

/* 模拟 */
public class CD142_2
{
    public static void solution(int a, int b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println(a + " " + b);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int a, b;
        a = in.nextInt();
        b = in.nextInt();
        solution(a, b);
    }
}

CD143 不用做任何比较判断找出两个数中较大的数

public class CD143_1
{
    public static int solution(int a, int b)
    {
        int c = a - b;
        int sa = sign(a);
        int sb = sign(b);
        int sc = sign(c);
        int difSab = sa ^ sb;
        int sameSab = flip(difSab);
        int returnA = difSab * sa + sameSab * sc;
        int returnB = flip(returnA);
        return a * returnA + b * returnB;
    }

    public static int flip(int n)
    {
        return n ^ 1;
    }

    public static int sign(int n)
    {
        return flip((n >> 31) & 1);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int a, b;
        a = in.nextInt();
        b = in.nextInt();
        System.out.println(solution(a, b));
    }
}

CD144 只用位运算不用算术运算实现整数的加减乘除运算⭐

/*⭐*/
public class CD144_1
{
    public static int solutionAdd(int a, int b)
    {
        int xor = a ^ b;
        int and = (a & b) << 1;
        if (and == 0) return xor;
        return solutionAdd(xor, and);
    }

    public static int solutionMinus(int a, int b)
    {
        return solutionAdd(a, solutionAdd(~b, 1));
    }

    public static int solutionTimes(int a, int b)
    {
        int ans = 0;
        while (b != 0)
        {
            if ((b & 1) == 1)
                ans = solutionAdd(ans, a);
            // a <<= 1;
            a = solutionAdd(a, a);
            b >>>= 1;
        }
        return ans;
    }

    public static int div(int a, int b)
    {
        int x = a < 0 ? solutionAdd(~a, 1) : a;
        int y = b < 0 ? solutionAdd(~b, 1) : b;
        int res = 0;
        for (int i = 31; i >= 0; i = solutionMinus(i, 1))
        {
            if ((x >> i) >= y)
            {
                res |= (1 << i);
                x = solutionMinus(x, y << i);
            }
        }
        return (a < 0) ^ (b < 0) ? solutionAdd(~res, 1) : res;
    }

    public static int solutionDiv(int a, int b)
    {
        if (b == 0) throw new RuntimeException("divisor is 0");
        if (a == Integer.MIN_VALUE && b == Integer.MIN_VALUE) return 1;
        else if (b == Integer.MIN_VALUE)  return 0;
        else if (a == Integer.MIN_VALUE)
        {
            int res = div(solutionAdd(a, 1), b);
            return solutionAdd(res, div(solutionMinus(a, solutionTimes(res, b)), b));
        }
        else
            return div(a, b);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int a, b;
        String op;
        String[] s = in.nextLine().split(" ");
        a = Integer.parseInt(s[0]);
        op = s[1];
        b = Integer.parseInt(s[2]);
        switch (op)
        {
            case "+":
                System.out.println(solutionAdd(a, b));
                break;
            case "-":
                System.out.println(solutionMinus(a, b));
                break;
            case "*":
                System.out.println(solutionTimes(a, b));
                break;
            case "\\":
                System.out.println(solutionDiv(a, b));
                break;
        }
    }
}

CD145 整数的二进制数表达中有多少个 1⭐

public class CD145_1
{
    public static int solution(int n)
    {
        int ans = 0;
        for (int i = 31; i >= 0; i--)
            if(((n>>>i)&1)==1)
                ans++;
        return ans;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println(solution(n));
    }
}

public class CD145_2
{
    public static int solution(int n)
    {
        int res = 0;
        while (n != 0)
        {
            n &= (n - 1);
            res++;
        }
        return res;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println(solution(n));
    }
}

/*⭐*/
public class CD145_3
{
    public static int solution(int n)
    {
        n = (n & 0x55555555) + ((n >>> 1) & 0x55555555);
        n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);
        n = (n & 0x0f0f0f0f) + ((n >>> 4) & 0x0f0f0f0f);
        n = (n & 0x00ff00ff) + ((n >>> 8) & 0x00ff00ff);
        n = (n & 0x0000ffff) + ((n >>> 16) & 0x0000ffff);
        return n;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println(solution(n));
    }
}

CD146 在其他数都出现偶数次的数组中找到出现奇数次的数

public class CD146_1
{
    public static int solution(int[] arr)
    {
        int ans = 0;
        for (int num : arr)
            ans ^= num;
        return ans;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
            arr[i] = in.nextInt();
        System.out.println(solution(arr));
    }
}

CD147 数组中只出现了一次的数字

public class CD147_1
{
    public static void solution(int[] arr)
    {
        int ans = 0, other = 0;
        for (int num : arr)
            ans ^= num;
        int rightOne = ans & (~ans + 1);
        for (int num : arr)
            if ((num & rightOne) != 0)
                other ^= num;
        System.out.println(Math.min(other, ans ^ other) + " " + Math.max(ans ^ other, other));
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
            arr[i] = in.nextInt();
        solution(arr);
    }
}

CD148 在其他数都出现 k 次的数组中找到只出现一次的数⭐

/*⭐⭐*/
public class CD148_1
{
    public static int solution(int[] arr, int k)
    {
        int[] temp = new int[32];
        for (int num : arr)
            bin2k(temp, num, k);
        return k2bin(temp, k);
    }

    public static int k2bin(int[] temp, int k)
    {
        int res = 0, base = 1;
        for (int i = 0; i < temp.length; i++)
        {
            res += base * temp[i];
            base *= k;
        }
        return res;
    }

    public static void bin2k(int[] temp, int num, int k)
    {
        int idx = 0;
        while (num != 0)
        {
            temp[idx] = (temp[idx] + num % k) % k;
            num /= k;
            idx++;
        }
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n, k;
        n = in.nextInt();
        k = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++)
            arr[i] = in.nextInt();
        System.out.println(solution(arr, k));
    }
}
posted @ 2023-11-21 16:27  Vivid-BinGo  阅读(5)  评论(0编辑  收藏  举报