使用java在base -2中创建二进制数组

Din*_*sha 0 java algorithm binary

我有一个数字值的函数:

val = sum{(A[i])*(-2)^i}
Run Code Online (Sandbox Code Playgroud)

因此,可以在st -8is [0,0,0,1]8is中完成表示位数组中的值[0,0,0,1,1].

请帮我写一个java方法,当我将一个十进制值传递给一个方法时,它将返回一个带有1和0的int数组,表示给定的十进制值.

Ex: when pass -8 , method returns [0,0,0,1].
    when pass 8 , method returns [0,0,0,1,1]. 
Run Code Online (Sandbox Code Playgroud)

11t*_*ion 5

基数-2中的-8应该[0,0,0,1][0,1,0,1]符合定义val = sum{(A[i])*(-2)^i}

以下是处理负面基础的代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Binary {

    public static void main(String[] args) {
        System.out.println("Started");
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter an integer : ");
        int num = Integer.parseInt(scan.nextLine());
        System.out.println("Enter base to convert to: ");
        int base = Integer.parseInt(scan.nextLine());

        scan.close();

        Integer[] bitArray = convertToBaseArray(num, base);

        System.out.println(Arrays.toString(bitArray));
    }

    private static Integer[] convertToBaseArray(int num, int base) {
        if(base == 0) {
            throw new RuntimeException("base can not be 0");
        } else if(num < 0 && base > 0) {
            throw new RuntimeException("positive base can not produce negative number");
        }
        List<Integer> bitList = new ArrayList<Integer>();

        int absoluteBase = Math.abs(base);
        if(base == 1) {
            //this is not unique, creating shortest bit array
            for(int i = 0; i < Math.abs(num); i++) {
                bitList.add(1);
            }
        } else if(base == -1) {
            //this is not unique, creating shortest bit array
            int evenBit = num > 0 ? 0 : 1;
            int oddBit = 1-evenBit;

            for(int i = 0; i < 2*Math.abs(num); i+= 2) {
                bitList.add(oddBit);
                bitList.add(evenBit);
            }
            if(oddBit == 1) {
                bitList.remove(bitList.size() -1);
            }
        } else {
            while(num != 0) {
                int remainder = num % base;
                if(remainder < 0) {
                    remainder += absoluteBase;
                    num -= absoluteBase;
                }
                num = num / base;
                bitList.add(remainder);
            }
        }
        return bitList.toArray(new Integer[]{});
    }

}
Run Code Online (Sandbox Code Playgroud)

以下是一些示例运行:

Started
Enter an integer : 
-8
Enter base to convert to: 
-2
[0, 0, 0, 1]

Started
Enter an integer : 
8
Enter base to convert to: 
-2
[0, 0, 0, 1, 1]

Started
Enter an integer : 
8
Enter base to convert to: 
-5
[3, 4, 1]

Started
Enter an integer : 
4
Enter base to convert to: 
1
[1, 1, 1, 1]

Started
Enter an integer : 
4
Enter base to convert to: 
-1
[1, 0, 1, 0, 1, 0, 1]

Started
Enter an integer : 
-4
Enter base to convert to: 
-1
[0, 1, 0, 1, 0, 1, 0, 1]
Run Code Online (Sandbox Code Playgroud)