Java 中队列和递归

public class DataConversion {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        int num = 24241;
        while (num > 0) {
            stack.push(num % 10);
            num = num / 10;
        }

        while (stack.size() > 0) {
            System.out.print(stack.pop());
        }

        System.out.println("--------------------------------");
        Deque<Integer> stack1 = new ArrayDeque<>();
        int number = 12;
        while (number > 0) {
            stack1.push(number % 8);
            number = number / 8;
        }

        while (stack1.size() > 0) {
            System.out.print(stack1.pop());
        }
    }
}

public class HanoiTest {
    public static long m = 0;

    public static void move(char ch1, long n, char ch2) {
        System.out.println("move " + n + " from " + ch1 + " to " + ch2);
    }

    public static void hanoi(int n, char A, char B, char C) {
        //System.out.println("times " + (++m) + " " + n + " " + A + " " + C);
        if (n == 1)
            move(A, 1, C);
        else {
            hanoi(n-1, A, C, B);
            move(A, n, C);
            hanoi(n-1, B, A, C);
        }
    }

    public static void main(String[] args) {
        hanoi(10, 'A', 'B', 'C');
    }
}

public class DataConversion {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        int num = 24241;
        while (num > 0) {
            stack.push(num % 10);
            num = num / 10;
        }

        while (stack.size() > 0) {
            System.out.print(stack.pop());
        }

        System.out.println("--------------------------------");
        Deque<Integer> stack1 = new ArrayDeque<>();
        int number = 12;
        while (number > 0) {
            stack1.push(number % 8);
            number = number / 8;
        }

        while (stack1.size() > 0) {
            System.out.print(stack1.pop());
        }

        System.out.println("-----------------------------------");
        System.out.println(fact(18));
        System.out.println("-----------------------------------");
        System.out.println(fib(2));
    }

    public static long fact(long n) {
        if (n == 0) return 1;
        else return fact(n-1) * n;
    }

    public static long fib(long n) {
        if (n == 0) {
            return 0;
        }
        else if (n == 1) {
            return 1;
        }
        else {
            return fib(n-1) + fib(n-2);
        }
    }
}

猜你喜欢

转载自tramp-zzy.iteye.com/blog/2230139