String permutations in Java (non-recursive)

后端 未结 1 1578
轮回少年
轮回少年 2021-01-12 13:39

I\'m a high school student of grade 10 trying to work through some problems in a Data Structures and Algorithms book on Java.

One of the questions is to print all p

1条回答
  •  爱一瞬间的悲伤
    2021-01-12 13:58

    Permutation with repetitions

    When you have n things to choose from ... you have n choices each time!

    When choosing r of them, the permutations are:

    n × n × ... (r times) = n^r

    I'm presenting 2 cases. First case is when the we know the size of n and r already, and its the easy one. The second when n and r are dynamic.

    //when n and r are known statically
    
    class Permutation
    {
        public static void main(String[] args)
        {
            char[] values = {'a', 'b', 'c', 'd'};
            int n = values.length;
            int r = 2; 
    
            int i = 0, j = 0;
            for(i=0; i

    0 讨论(0)
提交回复
热议问题