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);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值