0
点赞
收藏
分享

微信扫一扫

算法的华丽应用:小球掉落问题

hwwjian 2023-09-21 阅读 38

递归? 题目描述:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?

这道题可以应用递归调用函数自身,每一次调用都更改一次“坐标”

public class Main {
private double TotalHeight = 100;       //定义初始落下高度,先说:此变量为计算总路径长度的
private double CurHeight = 50;      //定义初始最终落点高度,先说:此变量是为记录最终落点高度的

public void drop(int times) {
if ((times - 1) == 10) {                  //定义times变量,相当于C语言里常说的flag(标记变量),记录次数
return;
}

setTotalHeight(getTotalHeight() + 2 * getCurHeight());
setCurHeight(getCurHeight() / 2);

drop(times +1);
}

public double getTotalHeight() {
return TotalHeight;
}

public void setTotalHeight(double totalHeight) {
TotalHeight = totalHeight;
}

public double getCurHeight() {
return CurHeight;
}

public void setCurHeight(double curHeight) {
CurHeight = curHeight;
}

public static void main(String[] args) {
Main main = new Main();
main.drop(2);
System.out.println("Total height is " + main.getTotalHeight());
System.out.println("Current height is " + main.getCurHeight());
}
}

都说算法枯燥,那我下面这样写,大家应该感兴趣吧: (MainFrame.java)

package GUI.swing.物理题.小球弹跳;

import javax.swing.JFrame;

import GUI.swing.panel.BallPanel;
import GUI.swing.time.Delay;

class Main {
    private double TotalHeight = 100;
    private double CurHeight = 50;

    public void drop(int times) {
        if ((times - 1) == 10) {
            return;
        }
        setTotalHeight(getTotalHeight() + 2 * getCurHeight());
        setCurHeight(getCurHeight() / 2);


        try {
            System.out.println("Total "+times+" height is " + getTotalHeight());
            System.out.println("Current "+times+" height is " + getCurHeight() + "\n");
            Thread.sleep(1000);//睡眠1秒。循环300次就是300秒也就是五分钟
                               //以后写线程大概此处填3000即可  --变速运动,没办法
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        drop(times + 1);
    }

    public double getTotalHeight() {
        return TotalHeight;
    }

    public void setTotalHeight(double totalHeight) {
        TotalHeight = totalHeight;
    }

    public double getCurHeight() {
        return CurHeight;
    }

    public void setCurHeight(double curHeight) {
        CurHeight = curHeight;
    }
}
public class MainFrame extends JFrame {
    public MainFrame(String title) {
        super(title);
    }

    public static void main(String[] args) {
        Delay delay = new Delay();
        Main main = new Main();

        MainFrame frame = new MainFrame("Hello JFrame");
        BallPanel ballPanel = new BallPanel();
        frame.add(ballPanel);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        delay.initDelay();

        ballPanel.setDelay(delay.getDelay());

        ballPanel.loopDrop(0);
        main.drop(2);

    }
}

(Delay.java)

package GUI.swing.time;

public class Delay {
    public void initDelay() {
        int g = 10;
        int i = 0;
        double s, t, t0 = 0.0;

        delay = new int[100];

        for (s = 100; s < 10100; s += 100) {
            t = Math.sqrt(2 * s / g);
            delay[i++] = (int) ((t - t0) * 100);
            t0 = t;
        }
    }

    public int[] getDelay() {
        return delay;
    }

    private int delay[];
}

(BallPanel.java)

package GUI.swing.panel;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class BallPanel extends JPanel {
    public BallPanel() {
        super();
    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.BLUE);
        g.fillOval(250, ballCenter, 30, 30);
    }

    public void loopDrop(int height) {
        int i;

        if (this.height == height) { // At bottom
            for (i = 0; i < targetHeight; i += MUL) {
                ballCenter = this.height - i;
                this.repaint();
                try {
                    Thread.sleep(delay[(targetHeight - i - 1) / MUL]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            loopDrop(this.height - i);
        } else { // At top
            for (i = height; i < this.height; i += MUL) {
                ballCenter = i;
                this.repaint();
                try {
                    Thread.sleep(delay[(i - height) / MUL]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            targetHeightV = targetHeightV / 2;
            targetHeight = targetHeightV;
            if (targetHeight != 0) {
                loopDrop(i);
            }
        }
    }

    public void setDelay(int delay[]) {
        this.delay = delay;
    }

    private int extracted() {
        return 100 * MUL;
    }

    private int targetHeight = extracted();

    private int targetHeightV = extracted();
    private int ballCenter = 0;
    private int height = extracted();
    private int delay[];

    private final int MUL = 4;
}


举报

相关推荐

0 条评论