There is an array for example [1,2,3], you need to get 1,11,12,13,111,112,113,122,123,133,2,22,23,222,223,233,3,33,333

You can simplify to get combinations of only a certain length, while doing this:

public class Test { private static void PFunction(int index[], int max){ for (int i=0; i<index.length; i++) index[i] = 0; int curIndex = index.length-1; while(true){ for (int anIndex : index) System.out.print(anIndex + " "); System.out.println(); for (int i=curIndex; i>=0; i--){ index[i]++; if (index[i] <= max) break; if (i==0) return; index[i] = 0; } } } public static void main(String[] args) { int index[] = new int[3]; PFunction(index, 3); } } 

But you need to get not all combinations, but only with unique values, that is, 0.0.3 and 3.0.0 and 0.3.0 - the same

Closed due to the fact that the essence of the issue is incomprehensible to the participants by pavel , Vladimir Martyanov , Streletz , Dmitriy Simushev , D-side 5 Sep '16 at 13:15 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    what is the problem? For simplicity, you can add an empty element to the array with a triple loop ... - pavel
  • The problem is that there will be a lot of elements - Aleksey
  • Specify it in the question itself. - edem
  • Well, in fact, I assumed a cycle like this: {i, 0, N} {j, i, N} {k, j, N} - pavel
  • 1. Put a restriction in the triple cycle, for example a [i] <= a [j] <= a [k], where i, j, k = 0..3 0-no numbers. 2. Add another array for block duplicates. We will not write for you. - nick_n_a

1 answer 1

There are many algorithms that can solve the problem (below is a link where several of them are described). I advise you to pay attention to the " Gray Codes " (this article is not ideal, but there is a lot of useful information).

Discrete mathematics, combinatorics .

  • Yes, this is a university assignment (I did the same myself a few years ago). The author does not know what "discrete mathematics" is and will "construct a bicycle." I told him that there is a section of science that solves "his problem" + gave a specific working algorithm to him. This is a completely "unambiguous" answer. - Phobophobia
  • @Dmitriy Simushev, nick_n_a You are too critical. Here you can cut off all the issues and say that they are university students, any question on SQL can be edited with the words "Oh, this is done at the institute on the databases, go read!" The author did not ask for it, and I think that Phobophobia gave an adequate answer, pointing out the necessary algorithm that can be used to solve this problem, of course, did not quite specify how it works, although it would be necessary. - Firepro September
  • @nick_n_a For requests addressed to the community, there is Meta . - Vladimir Gamalyan September
  • not necessarily that the author from uni. Perhaps he is still in school or an adult already mastering it all. - Phobophobia
  • 2
    @ Alexey, to order work, perhaps nothing more is needed. For this resource, it is advisable to give an example of attempts to solve it - mkkik