Blue Bridge Cup basic exercises to predict the road of the Blue Bridge Cup

Exam questions basic practice prediction

It is said that there are all kinds of rabbits and turtles in this world, but the study found that all rabbits and turtles have a common feature-like to race. So the tortoise and rabbit races are constantly happening in all corners of the world. Xiaohua is very interested in this, so he decides to study the race of different rabbits and tortoises. He found that although rabbits run faster than turtles, they have a well-known problem-proud and lazy, so in the game with the turtles, once the rabbit finds itself ahead of t meters or more after any second, they will stop Rest for s seconds. For different rabbits, the values ​​of t and s are different, but all turtles are the same-they will not stop until the end.
  However, some games are quite long, and it takes a lot of time to watch the whole process. Xiaohua found that as long as the data of rabbits and turtles 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). Speed ​​v2, and the corresponding t, s value of the rabbit, and the length of the track l-can predict the outcome of the game. But Xiaohua was lazy and did n’t want to guess the result of the game by hand calculation, so he found you-a high-level student in the computer department of Tsinghua University-asked for help, please write a program for the input of the game data v1, v2, t, s, l, predict the outcome of the game.
The input format
  input is only one line, including five positive integers v1, v2, t, s, l separated by spaces, where (v1, v2 <= 100; t <= 300; s <= 10; l <= 10000 and It is a common multiple of v1 and v2). The
output format
  contains two lines. The first line outputs the match result-a capital letter "T" or "R" or "D", which means that the turtle won, the rabbit won, or both arrived at the same time end.
  The second line outputs a positive integer, indicating the time (in seconds) it takes the winner (or both parties) 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:
From the conditions given in the question, it can be concluded that the turtle's time is fixed, and only the rabbit's time is uncertain. How to determine the rabbit's time according to a fixed distance? You can think about it in reverse thinking, how to determine the distance according to the time of the rabbit?

1. The first idea is very simple, assuming that the time of the rabbit and the turtle is the same, that is, regardless of whether the rabbit has passed the end point, before the turtle reaches the end point, it has always followed the rule of motion, by comparing the distance of the rabbit at the same time over the turtle sum determines whether to stop or continue to achieve the goal. When the turtle reaches the finish line, if sum> 0, the rabbit runs far and the rabbit wins, sum == 0, the same distance, and win simultaneously, sum <0, the turtle wins. But the disadvantage of this method is that the time for the rabbit to exercise must be calculated separately.
2. The normal idea is to directly calculate the time of the rabbit, and calculate the final result through a cycle (provided that the distance between the two is less than the distance).

#include<bits/stdc++.h>

using namespace std;

int main() {
	int v1,v2,t,s,l;
	cin >> v1 >> v2 >> t >> s >> l;
	int t1,t2,l2;
	t2 = l / v2;
	int v = v1 - v2;
	int sum = 0;
	for (int i = 0; i < t2; i++) {
		sum += v;
		l2 += v1;
		t1 ++;
		if(l2 > l){
			break;
		}
		if(sum >= t){
			i += s;
			t1 += s;
			sum = sum - (min(s,t2 - i - 1)) * v2;
		}			
	}
	if (l2 < l) {
		while(l2 < l) {
			l2 += v1;
			t1 += 1;
		}
	}
	if (t2 == t1){
		cout << "D" << endl << t2 << endl;
	}
	else if (t2 < t1) {
		cout << "T" << endl << t2<< endl;
	}
	else {
		cout << "R" << endl << t1<< endl;
	} 
	return 0;
} 

The above is the first method, the results are okay, I do n’t know why there are a few submissions (the output is exactly the same!), It is a bit magical (please correct me if you understand the big guys, I am grateful!), So I changed to the second The methods are as follows:

#include<bits/stdc++.h>

using namespace std;

int main() {
	int v1,v2,t,s,l;
	cin >> v1 >> v2 >> t >> s >> l;
	int t1 = 0, t2 = 0, l1 = 0, l2 = 0;
	while(l1 < l && l2 < l){
        if(l1 - l2 >= t){
            t2 += s;
            l1 = t1 * v1;
            l2 = t2 * v2;
        }
        else{
            t1++;
            t2++;
            l1 = t1 * v1;
            l2 = t2 * v2;
        }
    }
	if (l1 == l2){
		cout << 'D' << endl << t2;
	}
	else if (l1 < l2) {
		cout << 'T' << endl << l/v2;
	}
	else {
		cout << 'R' << endl << t2;
	} 
	return 0;
} 
Published 4 original articles · praised 3 · visits 78

Guess you like

Origin blog.csdn.net/HERODING23/article/details/105594550