Realization of tortoise and rabbit race prediction with C + +

Posted by JoeBuntu on Sat, 28 Mar 2020 12:13:34 +0100

Problem description
There are all kinds of rabbits and tortoises in the world, but research has found that all rabbits and tortoises have a common feature - like running. Therefore, tortoise and hare competitions are taking place in every corner 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 rabbits run faster than tortoises, they have a well-known problem - Pride and laziness. So in the competition with tortoises, once the rabbits find that they are ahead of t meters or more at the end of any second, they will stop and rest for s seconds. For different rabbits, the values of t and s are different, but all tortoises are the same - they never stop until they reach the end.
However, some races are quite long and it will take a lot of time to watch the whole race. Xiaohua found that as long as he recorded the data of rabbits and tortoises after the start of each race - the speed of rabbits v1 (which means that rabbits can run v1 meters per second), the speed of tortoises v2, and the corresponding T, s values of rabbits, and the length of the track l - he could predict the results of the race. But Xiaohua is lazy and doesn't want to guess the result of the match by hand. So he found you, a talented student in the computer department of Tsinghua University, and asked for help. Please write a program to predict the result of the match for the entered data v1, v2, t, s, l.
Input format
The input has 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 the common multiple of v1,v2)
Output format
The output consists of two lines. The first line outputs the result of the match - a capital letter "T" or "R" or "D", which means the tortoise wins, the rabbit wins, or both reach the end at the same time.
The second line outputs a positive integer indicating the time (in seconds) it takes the winner (or both parties) to reach the destination.
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
 
Train of thought: the key to this question is that once the rabbit finds that it has run t meters more than the tortoise, it will stop and rest for s seconds. That's not to say that the rabbit doesn't rest, but at the moment when it was ready to rest, go back s × v1 meters, then don't rest, go forward and make up the distance.
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class race
 5 {
 6 public:
 7     int get_v1()
 8     {
 9         cin >> v1;
10         return v1;
11     }
12     int get_v2()
13     {
14         cin >> v2;
15         return v2;
16     }
17     int get_t()
18     {
19         cin >> t;
20         return t;
21     }
22     int get_s()
23     {
24         cin >> s;
25         return s;
26     }
27     int get_l()
28     {
29         cin >> l;
30         return l;
31     }
32     void race_progress()
33     {
34         int i = 0;
35         int s1 = 0;
36         int s2 = 0;
37         while (s1 < l && s2 < l)
38         {
39             s1 = s1 + v1;
40             s2 = s2 + v2;
41             i++;
42             if (s1 >= l || s2 >= l)
43             {
44                 break;
45             }
46             if (s1 - s2 >= t)
47             {
48                 s1 = s1 - s * v1;
49             }
50         }
51         if (s1 > s2)
52         {
53             cout << "R" << endl;
54             cout << i;
55         }
56         else if (s2 > s1)
57         {
58             cout << "T" << endl;
59             cout << i;
60         }
61         else
62         {
63             cout << "D" << endl;
64             cout << i;
65         }
66     }
67 
68 private:
69     int v1, v2, t, s, l;
70 };
71 
72 int main(void)
73 {
74     race x;
75     x.get_v1();
76     x.get_v2();
77     x.get_t();
78     x.get_s();
79     x.get_l();
80     x.race_progress();
81     return 0;
82 }

Other simulation methods on the Internet:

Original link: https://blog.csdn.net/Qi2456/article/details/88100552

  1 #include<bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5  
  6 
  7 int main()
  8 
  9 {
 10 
 11     int v1,v2,t,s,l;
 12 
 13     cin>>v1>>v2>>t>>s>>l;
 14 
 15     int num=0,s1=0,s2=0;
 16 
 17     while(1)
 18 
 19     {
 20 
 21  
 22 
 23         if(s1>=l&&s2>=l)
 24 
 25         {
 26 
 27             cout<<"D"<<endl;
 28 
 29             cout<<num;
 30 
 31             break;
 32 
 33         }
 34 
 35         else if(s1>=l)
 36 
 37         {
 38 
 39             cout<<"R"<<endl;
 40 
 41             cout<<num;
 42 
 43             break;
 44 
 45         }
 46 
 47         else if(s2>=l)
 48 
 49         {
 50 
 51             cout<<"T"<<endl;
 52 
 53             cout<<num;
 54 
 55             break;
 56 
 57         }
 58 
 59         if(s1-s2>=t)
 60 
 61         {
 62 
 63             for(int i=0;i<s;i++)
 64 
 65             {
 66 
 67                 num++;
 68 
 69                 s2+=v2;
 70 
 71                 if(s2>=l)
 72 
 73                 {
 74 
 75                     cout<<"T"<<endl;
 76 
 77                     cout<<num;
 78 
 79                     exit(0);
 80 
 81                 }
 82 
 83             }
 84 
 85  
 86 
 87         }
 88 
 89         else
 90 
 91         {
 92 
 93             num++;
 94 
 95         s1+=v1;
 96 
 97         s2+=v2;
 98 
 99         }
100 
101     }
102 
103     return 0;
104 
105 }

 

Topics: C++ REST