求两道简单java题目答案,在线等

2024-11-22 23:56:56
推荐回答(1个)
回答1:

1.
public class HaNuoTa
{
static long s=0;
public static void main(String args[])
{
System.out.println("请输入盘子的数量:");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.println("移动方案为:" );
hanoi(n, 'a', 'b', 'c');
System.out.println("需要移动次数:"+s);
}
static void hanoi(int n, char a, char b, char c)
{
if (n > 0) {
hanoi(n - 1, a, c, b);
move(a, b);
hanoi(n - 1, c, b, a);
s++;
}
}
static void move(char x, char y)
{
System.out.println(x + "->" + y + "\t");
}
}

2.
public class Fibonacci
{
public static int Fibonacci(int n)
{
if(n==0 || n==1) {
return 1;
} else {
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
public static void main(String args[])
{
System.out.println("请输入n的值:");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.println(Fibonacci(n));
}
}