`
书音棋
  • 浏览: 142599 次
  • 性别: Icon_minigender_1
  • 来自: 哈尔滨
社区版块
存档分类
最新评论

java 经典算法(转载)

    博客分类:
  • java
阅读更多

1. 用1、2、3、4、5这五个数字,用java写一个main函数,打印出所有不同的排列,如:51234、41235等。 

  1. public class TestQuestion {  
  2. static int[] bits = new int[] { 12345 };  
  3.   
  4. /** 
  5. * @param args 
  6. */  
  7. public static void main(String[] args) {  
  8. sort("", bits);  
  9. }  
  10.   
  11. private static void sort(String prefix, int[] a) {  
  12. if (a.length == 1) {  
  13. System.out.println(prefix + a[0]);  
  14. }  
  15.   
  16. for (int i = 0; i < a.length; i++) {  
  17. sort(prefix + a[i], copy(a, i));  
  18. }  
  19. }  
  20.   
  21. private static int[] copy(int[] a,int index){  
  22. int[] b = new int[a.length-1];  
  23. System.arraycopy(a, 0, b, 0, index);  
  24. System.arraycopy(a, index+1, b, index, a.length-index-1);  
  25. return b;  
  26. }  
  27. }  
  28. 2. 对第一题增加难度,用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。 

    基本思路: 
    2-1. 把问题归结为图结构的遍历问题。图遍历思想:实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。 

    2-2. 显然这个结果集还未达到题目的要求。从以下几个方面考虑:                                                 

        2-2-1.   3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。 
        2-2-2.   不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果 
        2-2-3.   4不能在第三位: 仍旧在结果集中去除满足此条件的结果。 

    采用二维数组定义图结构,最后的代码是: 
    1. import java.util.Iterator;  
    2. import java.util.TreeSet;  
    3.   
    4. public class TestQuestion {  
    5.   
    6. private String[] b = new String[]{"1""2""2""3""4""5"};  
    7. private int n = b.length;  
    8. private boolean[] visited = new boolean[n];  
    9. private int[][] a = new int[n][n];  
    10. private String result = "";  
    11. private TreeSet set = new TreeSet();  
    12.   
    13. public static void main(String[] args) {  
    14. new TestQuestion().start();  
    15. }  
    16.   
    17. private void start() {  
    18.   
    19. // Initial the map a[][]  
    20. for (int i = 0; i < n; i++) {  
    21. for (int j = 0; j < n; j++) {  
    22. if (i == j) {  
    23. a[i][j] = 0;  
    24. else {  
    25.     a[i][j] = 1;  
    26. }  
    27. }  
    28. }  
    29.   
    30. // 3 and 5 can not be the neighbor.  
    31. a[3][5] = 0;  
    32. a[5][3] = 0;  
    33.   
    34. // Begin to depth search.  
    35. for (int i = 0; i < n; i++) {  
    36.     this.depthFirstSearch(i);  
    37. }  
    38.   
    39. // Print result treeset.  
    40. Iterator it = set.iterator();  
    41. while (it.hasNext()) {  
    42. String string = (String) it.next();  
    43. // "4" can not be the third position.  
    44. if (string.indexOf("4") != 2) {  
    45. System.out.println(string);  
    46. }  
    47. }  
    48. }  
    49.   
    50. private void depthFirstSearch(int startIndex) {  
    51. visited[startIndex] = true;  
    52. result = result + b[startIndex];  
    53. if (result.length() == n) {  
    54. // Filt the duplicate value.  
    55. set.add(result);  
    56. }  
    57. for(int j = 0; j < n; j++) {  
    58. if (a[startIndex][j] == 1 && visited[j] == false) {  
    59. depthFirstSearch(j);  
    60. else {  
    61. continue;  
    62. }  
    63. }  
    64.   
    65. // restore the result value and visited value after listing a node.  
    66.     result = result.substring(0, result.length() -1);  
    67.     visited[startIndex] = false;  
    68. }  
    69. }  
    70. 以上引用原址:http://www.blogjava.net/SongJunke/articles/101741.html 

      3.   递归思想。递归要抓住的关键是:(1)递归出口;(2)地推逐步向出口逼近。 

      3-1. 汉诺塔 

        这是递归的超经典的例子,几乎每本程序设计书上谈到递归都会介绍。具体情景不再赘述。以我上述的方法观之: 

          3-1-1. 递归的出口在于disk数为一的时候 

          3-1-2. 向出口逼近:如果不是一,是n ,则我们先挪动上面n-1块disk,等上面挪完,即递归返回的时候,我们挪动最底下的disk. 

        仅仅如此,一个貌似十分复杂的问题就解决了,因为挪动那n-1块disk的时候,会继续向上减少,直到disk的数量为一为止。下面给出代码: 
    1. import javax.swing.JOptionPane;   
    2. public class Hanoi{   
    3. private static final String DISK_B = "diskB";   
    4. private static final String DISK_C = "diskC";   
    5. private static final String DISK_A = "diskA";   
    6. static String from=DISK_A;   
    7. static String to=DISK_C;   
    8. static String mid=DISK_B;   
    9. public static void main(String[] args) {   
    10. String input=JOptionPane.showInputDialog("please input the number of the disks you want me move.");   
    11. int num=Integer.parseInt(input);   
    12. move(num,from,mid,to);   
    13. }   
    14. private static void move(int num, String from2, String mid2, String to2) {   
    15. if(num==1){   
    16. System.out.println("move disk 1 from "+from2+" to "+to2);   
    17. }   
    18. else {   
    19. move(num-1,from2,to2,mid2);   
    20. System.out.println("move disk "+num+" from "+from2+" to "+to2);   
    21. move(num-1,mid2,from2,to2);   
    22. }   
    23. }   
    24. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics