List of all binary combinations for a number in Java

后端 未结 6 1060
后悔当初
后悔当初 2021-01-14 05:15

I am working on a project involving \"Dynamic Programming\" and am struck on this trivial thing, please help.

Suppose I take 4 as an input, I want to display somethi

6条回答
  •  温柔的废话
    2021-01-14 05:55

    here is the code is to find the combination

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package rotateimage;
    
    /**
     *
     * @author ANGEL
     */
    public class BinaryPermutaion {
    
        public static void main(String[] args) {
            //object creation
            BinaryPermutaion binaryDigit=new BinaryPermutaion();
            //Recursive call of the function to print the binary string combinations
            binaryDigit.printBinary("", 4);
        }
    
        /**
         * 
         * @param soFar String to be printed
         * @param iterations number of combinations
         */
        public void printBinary(String soFar, int iterations) {
        if(iterations == 0) {
            System.out.println(soFar);
        }
        else {
            printBinary(soFar + "0", iterations - 1);
            printBinary(soFar + "1", iterations - 1);
        }
    }
    }
    

提交回复
热议问题