然而有些比赛相当漫长,全程观看会耗费大量时间,而小华发现只要在每场比赛开始后记录下兔子和乌龟的数据——兔子的速度v1(表示每秒兔子能跑v1米),乌龟的速度v2,以及兔子对应的t,s值,以及赛道的长度l——就能预测出比赛的结果。但是小华很懒,不想通过手工计算推测出比赛的结果,于是他找到了你——清华大学计算机系的高才生——请求帮助,请你写一个程序,对于输入的一场比赛的数据v1,v2,t,s,l,预测该场比赛的结果。
第二行输出一个正整数,表示获胜者(或者双方同时)到达终点所耗费的时间(秒数)。
4
3
4
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int v1 = sc.nextInt();
int v2 = sc.nextInt();
int t = sc.nextInt();
int s = sc.nextInt();
int l = sc.nextInt();
int s1 = 0;
int s2 = 0;
int time = 0;
while (s1 < l && s2 < l) {
if (s1 - s2 >= t) {// 兔子停
if ((l - s2) / v2 < s) {// 乌龟先到达终点
time += (l - s2) / v2;
s2 = l;
} else {// 乌龟不能到达终点
time += s;
s2 += v2 * s;
}
} else {// 兔子不停
time++;
s1 += v1 * 1;
s2 += v2 * 1;
}
}
if (s1 > s2) {
System.out.println("R");
System.out.println(time);
} else if (s1 < s2) {
System.out.println("T");
System.out.println(time);
} else {
System.out.println("D");
System.out.println(time);
}
}
}
}
1万+



被折叠的 条评论
为什么被折叠?



