蓝桥杯龟兔赛跑javaBASIC_24

 遇题不决,先画草图
注释比较详细,仔细琢磨应该能看懂

import java.util.Scanner;

public class Main {
    private static int R_l = 0, T_l = 0, S = 0;//R_L兔子跑的路程;T_l乌龟跑的路程;S最终结果时间。

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int v1 = sc.nextInt();//兔子速度
        final int v2 = sc.nextInt();//乌龟速度
        final int t = sc.nextInt(); //相差距离
        final int s = sc.nextInt(); //兔子等待时间
        final int l = sc.nextInt(); //总路程

        int a = run(v1, v2, t, s, l);//随便一个字母(a) 接一下return。

        //判断结果
        if (R_l > T_l) {
            System.out.println("R");
            System.out.println(a);
        } else if (T_l > R_l) {
            System.out.println("T");
            System.out.println(a);
        } else {
            System.out.println("D");
            System.out.println(a);
        }

    }

    /**
     * @param v1 兔子速度
     * @param v2 乌龟速度
     * @param t  相差距离
     * @param s  兔子等待时间
     * @param l  总路程
     * @return 共用了多长时间,即最终结果
     */
    static int run(int v1, int v2, int t, int s, int l) {
        //共同跑1s,并记录在S。
        R_l += v1 * 1;
        T_l += v2 * 1;
        S++;

        int temp = R_l - T_l;//临时存储相对距离
        while (R_l < l && T_l < l) {
            //相对距离小于t,递归继续共同跑
            if (temp < t) {
                run(v1, v2, t, s, l);
            } else {
                //这里是预防出现兔子在等乌龟时,s没到时间乌龟已经到了终点,所以每秒判断一次乌龟是否到了终点
                //例如:兔子在等乌龟,乌龟只用了s/2就到了终点,结果应该是S+s/2,而不是S+s。
                for (int i = 0; i < s; i++) {
                    T_l += v2 * 1;
                    S++;
                    if (T_l >= l)
                        break;
                }
                //判断相对距离,如果>t,兔子接着等
                temp = R_l - T_l;
            }
        }
        return S;
    }
}

大学新生,有问题请指出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您介绍一下 Java 编写的龟兔赛跑模拟。 首先,我们需要定义两个角色——乌龟和兔子,他们的基本属性包括速度、位置等信息。可以用一个类来表示他们: ```java public class Animal { private String name; // 名称 private int speed; // 速度 private int position; // 位置 // 构造函数 public Animal(String name, int speed) { this.name = name; this.speed = speed; this.position = 0; } // 睡觉 public void sleep() { System.out.println(name + "在睡觉..."); } // 行动 public void move() { int distance = speed; // 行进距离等于速度 position += distance; // 更新位置 System.out.println(name + "跑了" + distance + "米,当前位置:" + position + "米"); } // 获取位置 public int getPosition() { return position; } } ``` 接下来,我们需要模拟比赛的场景。可以使用一个类来表示比赛,包括两个角色和比赛的逻辑: ```java public class Race { private Animal tortoise; // 乌龟 private Animal hare; // 兔子 // 构造函数 public Race() { tortoise = new Animal("乌龟", 5); hare = new Animal("兔子", 10); } // 开始比赛 public void start() { int totalDistance = 100; // 总距离 while (tortoise.getPosition() < totalDistance && hare.getPosition() < totalDistance) { tortoise.move(); hare.move(); if (hare.getPosition() - tortoise.getPosition() > 10) { tortoise.sleep(); // 兔子领先10米以上,乌龟睡觉 } } System.out.println("比赛结束,"); if (hare.getPosition() >= totalDistance && tortoise.getPosition() >= totalDistance) { System.out.println("双方同时到达终点,平局!"); } else if (hare.getPosition() >= totalDistance) { System.out.println("兔子获胜!"); } else { System.out.println("乌龟获胜!"); } } } ``` 最后,我们可以在 `main()` 函数中创建比赛对象并开始比赛: ```java public static void main(String[] args) { Race race = new Race(); race.start(); } ``` 这样,我们就成功地用 Java 编写了一个龟兔赛跑模拟程序。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值