@递归算法
汉诺塔
public class Hanoilmpl {
public static void hanoi(int n, char A, char B, char C) {
if (n == 1) {
move(A, C);
} else {
hanoi(n - 1, A, C, B);//将n-1个盘子由A经过C移动到B
move(A, C); //执行最大盘子n移动
hanoi(n - 1, B, A, C);//剩下的n-1盘子,由B经过A移动到C
}
}
private static void move(char A, char C) {//执行最大盘子n的从A-C的移动
System.out.println("move:" + A + "--->" + C);
}
public static void main(String[] args) {
System.out.println("移动汉诺塔的步骤:");
hanoi(3, 'a', 'b', 'c');
}
}
数组
a0=1,a1=1,a2=2,a3=3,a4=5,a5=8,…
从第三个元素开始 ai=a(i-1)+a(i-2)
求解 an
long a_pre_pre = 1;
long a_pre = 1;
long an = 0;
private void GetValue(int currentIndex, int n)
{
if (n >= 2 && currentIndex < n)
{
an = a_pre + a_pre_pre;
currentIndex++;
a_pre_pre = a_pre;
a_pre = an;
GetValue(currentIndex,n);
}
else
{
//an = 1;
}
}
GetValue(2,13); 233