Java_CH8_HW2


  1. import java.io.*;
  2. import java.lang.reflect.Array;
  3. public class Java_CH8_HW2
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. // TODO 自動產生的方法 Stub
  9. char byte1[];
  10. byte1 = new char[0];
  11. int n1 = 0;
  12. String s = "0000";
  13. BufferedReader in = new BufferedReader(
  14. new InputStreamReader(System.in));
  15. try
  16. {
  17. System.out.println("Enter:");
  18. s = in.readLine();
  19. n1 = s.length();
  20. byte1 = new char[n1];
  21. s.getChars(0, n1, byte1, 0); // 將讀入的 n1 個字元,放進 byte1 字元陣列。
  22. }
  23. catch (Exception e)
  24. {
  25. System.out.println("Error:" + e.toString());
  26. }
  27.  
  28. Permutation perm1 = new Permutation(); // 建立排列計算物件
  29. perm1.N = n1; // 設定字元數
  30. perm1.perm(byte1, 0, ""); // 第一個參數為輸入字串,第二個參數 0 為陣列第一個位置
  31. }
  32.  
  33. }
  34.  
  35. class Permutation
  36. {
  37. public int N;
  38. void perm(char[] num, int index, String str)
  39. {
  40. if (index >= num.length)
  41. System.out.println(str);
  42. else
  43. {
  44. for (int i = 0; i < num.length; i++)
  45. if (str.indexOf(num[i]) == -1)
  46. perm(num, index + 1, str + " " + num[i]);
  47. }
  48. }
  49. }
  50.  

留言

這個網誌中的熱門文章

C# 井字遊戲