/**

  • 全排列问题
  • */

public class Permuting { static ArrayList x = new ArrayList(); static int N; static int cnt = 0; static int t;

  1. public static void main(String args[]) {
  2. int n = 10;
  3. Times.test("当n = " + n + "时候的耗费时间", new Times.Task() {
  4. public void execute() {
  5. PermutingCaller(n);
  6. }
  7. });
  8. }
  9. private static void PermutingCaller(int n) {
  10. N = n;
  11. for (int i = 0; i < n; ++i)
  12. x.add(i + 1);
  13. Permuting(0);
  14. }
  15. private static void Permuting(int i) {
  16. if (i < N - 1) {
  17. for (int j = i; j < N; j++) {
  18. t = x.get(i);
  19. x.set(i, x.get(j));
  20. x.set(j, t);
  21. Permuting(i + 1);
  22. t = x.get(i);
  23. x.set(i, x.get(j));
  24. x.set(j, t);
  25. }
  26. }
  27. else
  28. OutputOnePermutation();
  29. }
  30. private static void OutputOnePermutation()
  31. {
  32. System.out.print(++cnt + " : ");
  33. for (int x : x)
  34. System.out.print(x + " ");
  35. System.out.println();
  36. }

} ```

n:10

n