fork(38) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static char[] pool = new char[]{'1', '2', '3'};
  11.  
  12. public static void buildStrings(char[] root, int length)
  13. {
  14. // allocate an array to hold our counts:
  15. int[] pos = new int[length];
  16. char[] combo = new char[length];
  17. for(int i = 0; i < length; i++)
  18. combo[i] = root[0];
  19.  
  20. while(true)
  21. {
  22. // output the current combinations:
  23. System.out.println(String.valueOf(combo));
  24.  
  25. // move on to the next combination:
  26. int place = length - 1;
  27. while(place >= 0)
  28. {
  29. if(++pos[place] == root.length)
  30. {
  31. pos[place] = 0;
  32. combo[place] = root[0];
  33. place--;
  34. }
  35. else
  36. {
  37. combo[place] = root[pos[place]];
  38. break;
  39. }
  40. }
  41. if(place < 0)
  42. break;
  43. }
  44. }
  45.  
  46. public static void main (String[] args) throws java.lang.Exception
  47. {
  48. // your code goes here
  49. buildStrings(pool, 2);
  50. }
  51. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
11
12
13
21
22
23
31
32
33