it_worker365

   ::  ::  ::  ::  :: 管理
import java.util.ArrayList;
import java.util.List;

/**
 * Created by itworker365 on 5/10/2017.
 */
public class digui {
    public static void main (String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        System.out.println(recursive(list, list.size() - 1));
        hanoi(3);
    }

    public static int recursive (List<Integer> list, int pos) {
        if (pos >= 2) {
            int sum = list.get(pos) + list.get(pos - 1);
            return sum + recursive(list, pos - 2);
        } else if (pos == 1) {
            return list.get(pos) + list.get(pos - 1);
        } else if (pos == 0) {
            return list.get(pos);
        } else {
            return 0;
        }
    }

    //汉诺塔问题,大小交叠的盘子如何用3个柱子轮换,如果只有一个直接换,两个借助中间柱子中转,再多就直接将n-1看成一块递归计算
    private static void hanoi (int n) {
        hanoiLogic(n, "A", "B", "C");
    }
    private static void hanoiLogic (int n, String A, String B, String C) {
        if (n == 1) {
            move(n, A, C);
        } else {
            hanoiLogic(n - 1, A, C, B);
            move(n, A, C);
            hanoiLogic(n - 1, B, A, C);
        }
    }
    private static void move (int n, String from, String to) {
        System.out.println("move from " + from + " to: " + to);
    }
}

 

posted on 2017-05-10 10:06  it_worker365  阅读(184)  评论(0编辑  收藏  举报