fork download
  1. import java.util.Arrays;
  2.  
  3. class SendfromHigherFrameToLowerFrame {
  4.  
  5. public static void main(String[] args) {
  6. int[] a = {1,2,3};
  7.  
  8. int[] stack = new int[3];
  9. int count = fun(a, stack, 0);
  10.  
  11. System.out.println("count == "+count);
  12. }
  13.  
  14. static int fun(int[] a, int[] stack, int frame) {
  15. if(a.length == frame) {
  16. print(stack);
  17. return 1;
  18. }
  19. int res = 0;
  20. for(int i = 0;i<a.length;i++) {
  21. //save stack
  22. int[] temp = new int[stack.length];
  23. save(stack, temp);
  24. if(isValid(stack, a[i]) == true) {
  25. stack[frame] = a[i];
  26. res += fun(a, stack, frame+1);
  27. //restore stack
  28. restore(temp, stack);
  29. }
  30. }
  31. return res;
  32. }
  33.  
  34. static void save(int[] source, int[] destination) {
  35. for(int i = 0;i<source.length;i++)
  36. destination[i] = source[i];
  37. }
  38.  
  39. static void restore(int[] source, int[] destination) {
  40. for(int i = 0;i<source.length;i++)
  41. destination[i] = source[i];
  42. }
  43.  
  44. static boolean isValid(int[] a, int key) {
  45. for(int i = 0;i<a.length;i++) {
  46. if(a[i] == key)
  47. return false;
  48. }
  49. return true;
  50. }
  51.  
  52. static void print(int[] a)
  53. {
  54. for(int x : a)
  55. System.out.print(x + " ");
  56. System.out.println();
  57. }
  58. }
  59.  
Success #stdin #stdout 0.12s 320576KB
stdin
Standard input is empty
stdout
1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 1 2 
3 2 1 
count == 6