Blue Bridge Cup Basic Practice - Tortoise and Hare Prediction

Problem Description
  It is said that there are all kinds of rabbits and tortoises in this world, but research has found that all rabbits and tortoises have one thing in common - they like to race. As a result, races between tortoises and rabbits are constantly taking place in all corners of the world. Xiaohua is very interested in this, so he decides to study the races between different rabbits and tortoises. He found that although the rabbits ran faster than the tortoises, they had a well-known problem - pride and laziness, so in a race against the tortoises, when the rabbits found themselves ahead of t meters or more after any second, they would stop Rest s seconds. The values ​​of t and s are different for different rabbits, but all turtles are the same - they never stop until the end.
  However, some games are quite long, and it will take a lot of time to watch the whole process, and Xiaohua found that as long as the data of the rabbit and the tortoise are recorded after the start of each game - the speed of the rabbit is v1 (meaning that the rabbit can run v1 meters per second), the speed of the tortoise is v1. The speed v2, and the corresponding values ​​of t and s for the rabbit, and the length of the track l - can predict the outcome of the race. But Xiaohua was lazy and didn't want to infer the result of the game by manual calculation, so he found you - a high-caliber student from the Department of Computer Science of Tsinghua University - asking for help, please write a program, for the input data v1 of a game, v2, t, s, l, predict the outcome of the game.
input format
  The input is only one line, containing five positive integers v1, v2, t, s, l separated by spaces, where (v1,v2<=100;t<=300;s<=10;l<=10000 and v1 , a common multiple of v2)
output format
  The output consists of two lines, the first of which outputs the result of the race - a capital "T" or "R" or "D", which means the turtle wins, the hare wins, or both reach the finish line, respectively.
  The second line outputs a positive integer representing the time (in seconds) it took for the winner (or both) to reach the finish line.
sample input
10 5 5 2 20
Sample output
D
4
sample input
10 5 5 1 20
Sample output
R
3
sample input
10 5 5 3 20
Sample output
T

         4

Idea: In an infinite loop in seconds, each layer of loop makes the rabbit and the tortoise run forward a corresponding distance. At the end of the cycle, it is detected whether the rabbit exceeds the tortoise t meters. If it exceeds, it will start to rest for s seconds, and then directly fast forward s. seconds, the turtle moves for s seconds, and the number of seconds increases, but note that at the end of each second you should check whether the end point is reached.

Code:

public class test {
	public static void main(String[] args) {
//v1, v2, t, s, l, leading t meters rest s seconds
		Scanner input = new Scanner(System.in);
		int v1 = input.nextInt(), v2 = input.nextInt(),
				t = input.nextInt(), s = input.nextInt(),
				l = input.nextInt();
		int lr = 0;//Rabbit distance
		int lt = 0;//turtle distance
		int i = 1;
		for(i = 1 ;; i++ ) {
			lt += v2;
			lr += v1;
			if(lr >= l || lt >= l)break;
			if(lr - lt >= t) {
				for(int j = 1 ; j <= s ; j++) {
					lt += v2;
					i ++;
					if(lt >= l)break;
				}
				
			}
			if(lr >= l || lt >= l)break;
		}
		if(lr > lt)System.out.println("R");
		if(lr < lt)System.out.println("T");
		if(lr == lt)System.out.println("D");
		System.out.println(i);
	}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325591688&siteId=291194637