赞
踩
斐波那契数:
- public class Der {
- public static void main(String[] args) {
- tool nue=new tool();
- int a=nue.Fibonacci(6);
- System.out.println(a);
-
-
- }
- }
- class tool{
- public int Fibonacci(int n){
- if (n >= 1) {
- if (n == 2 || n == 1)
- return 1;
- else
- return Fibonacci(n-1)+Fibonacci(n-2);//斐波那契数从第三个开始就是前两位数之和为该位数
- }
- return 0;
- }
-
- }
- }

- //猴子吃桃,一天吃总数的一半多一个,第十天还有1个
- public class Der {
- public static void main(String[] args) {
- int day01=1;
- Monkey su=new Monkey();
- int v=su.Peach(day01);
- System.out.println(v);
-
-
-
- }
- }
- class Monkey{
- public int Peach(int n){
- if (n==10)//如果就是最后一天那么就时1个桃
- return 1;
- else if (n>=1&&n<=9)//十天这内
- return( Peach((n+1))+1)*2;//代表的是后一天(n+1),Peach(n+1)代表的是后一天的桃
- //( Peach((n+1))+1)*2前一天的桃=(后一天的桃+1)*2
- else
- return -1;
-
-
-
- }
- }

- //走迷宫
- public class Der {
- public static void main(String[] args) {
- int map[][] = new int[8][7];
- //布置地图
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 7; j++) {
- map[0][j] = 1;
- map[i][0] = 1;
- map[7][j] = 1;
- map[i][6] = 1;
-
- }
- }
- map[1][2] = 1;
- map[3][1] = 1;
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 7; j++)
- System.out.print(map[i][j]);
- System.out.println();
- }
- Secret su = new Secret();
- boolean v = su.Route(map, 1, 1);
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 7; j++)
- System.out.print(map[i][j]);
- System.out.println();
-
-
- }
- }
- }
- class Secret{
- //设置0为可以走,1为障碍物,2为可以走且已走,3为走了但为死路
- public boolean Route(int a[][],int i,int j)
- {
- if (a[6][5]==2)//预设的终点,如果终点为2代表已经走到了。
- {
- return true;
-
- }else {
- if (a[i][j]==0)//如果当前的位置为可以走的点的话
- {
- a[i][j] = 2;//那么就将当前位置标记为2,为已走的位置
-
- if (Route(a, i + 1, j))//然后从当前位置向下一个走,判断是否可以走
- return true;
- else if (Route(a, i, j + 1))//再判断右边
- return true;
- else if (Route(a, i - 1, j))//上边
- return true;
- else if (Route(a, i, j - 1))//左边
- return true;
- else {
- a[i][j] = 3;//如果当前位置不可走则设置为3,走过为死路
- return false;
- }
- }
- else
- return false;
- }
-
- }
- }

- //汉诺塔
- public class Der {
- public static void main(String[] args) {
- tower hanoitower=new tower();
- hanoitower.Hanoitower(5,'A','B','C');
- }
- }
- class tower{
- //num表示需要移动卡片的个数
- //a,b,c表示移动的三根柱子
- public void Hanoitower(int num,char a,char b,char c){
-
- if (num==1)//如果个数为一个,则直接把a柱上的移动到c
- System.out.println(a+"->"+c);
- else {
- Hanoitower(num-1,a,c,b);//num-1表示总卡牌除了最下面的一个之和
- //要想把最后一个先移到c柱就得先移动前面的卡牌,那么就需要借助b达到目的
- System.out.println(a+"->"+c);//将a最后一个移到c
- Hanoitower(num-1,b,a,c);//b中移到a借助c
-
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。