public class High {

static float h = 100;
static float longs = 100;

public static void main(String[] args) {
new High().jump();

System.out.println(longs);
System.out.print(h);
}

public void jump(){
for(int i=1 ; i<10; i++){
h = h/2;
longs += 2*h;

}
}}
请教如何把此程序改成递归算法呢,想了半天不得其解

解决方案 »

  1.   

    public class High { static float h = 100; 
    static float longs = 100; public static void main(String[] args) { 
    new High().jump(10); 
    System.out.println(longs); 
    System.out.print(h); 
    } public void jump(int len){ 
    if(len==1) return;
    h = h/2; 
    longs += 2*h; 
    jump(len-1);
    } } 
      

  2.   

    public class High {     static float h = 100; 
        static float longs = 100;     public static void main(String[] args) { 
            new High().jump(1);         System.out.println(longs); 
            System.out.print(h); 
        }     public void jump(int a){ 
            if (a < 10) {
                h = h/2; 
                longs += 2*h; 
                jump(a+1);
            }
        } }