package com.demo.calculate;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private int[] arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_fibonacci).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int fibonacci = fibonacci(4);
Log.i("tag",fibonacci+"====fibonacci=====");
}
});
findViewById(R.id.btn_hanoi).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hanio(3,'A','B','C');
}
});
}
/**
* 汉诺塔问题
*/
private void hanio(int n,char from,char in,char to) {
if (n<=0){
return;
}
if (n==1){
Log.i("tag","把第1个盘子从"+from+"移动到"+to);
}else{
//移动上面所有的盘子到中间位置
hanio(n-1,from,to,in);
Log.i("tag","把第"+n+"个盘子从"+from+"移动到"+to);
//把中间位置的所有的盘子移动到目标位置
hanio(n-1,in,from,to);
}
}
/**
* 斐波那契数列
*/
private int fibonacci(int n) {
if (n<0){
return -1;
}else if(n ==0 || n ==1){
return 1;
}else{
return fibonacci(n-1)+fibonacci(n-2);
}
}
}
网友评论