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 t, s values ​​of 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 has 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 end.
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

code show as below:
#include <stdio.h>
intmain()
{
    int v1,v2,t,s,l; //v1 is the speed of the rabbit v2 is the speed of the tortoise when the rabbit leads t and rests for s seconds. The length of the runway is l
    scanf("%d%d%d%d%d",&v1,&v2,&t,&s,&l);
    int sum1=0,sum2=0,time=0,flag=0; //sum1 total distance of the rabbit and sum2 of the total distance of the tortoise. The total time flag is used to calculate the waiting time
    while( sum1<l && sum2<l )
    {
	if(sum1-sum2>=t && flag == 0)
	{
		flag = s;
	}
	if(flag==0)
	{
		sum1 += v1;
	}
	else flag--;
	sum2 += v2;
	time++;
     }
 if(sum1 == sum2) printf("D");
	else if(sum1 > sum2) printf("R");
	else printf("T");
	printf("\n%d",time);
    return 0;
}

Guess you like

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