Home >Backend Development >C++ >C++ program to find winner of typing game after delay time
Suppose we have five numbers s, v1, v2, t1 and t2. Amal and Bimal are playing a typing game, which they play online. In this game, they will enter a string of length s. Amal enters a character in v1 milliseconds, and Bimal enters a character in v2 milliseconds. Amal's network delay is t1 milliseconds, and Bimal's network delay is t2 milliseconds.
If the connection delay is t milliseconds, the game proceeds as follows:
After t milliseconds after the game starts, the participant receives the text to enter.
Then he started typing.
T milliseconds after he finished inputting all the text, the website received the relevant information.
Whoever finishes first is the winner. If both participants have the same time, it is a tie. We need to find the winner.
So if the input is s = 5; v1 = 1; v2 = 2; t1 = 1; t2 = 2, then the output will be Amal, because Amal's success message arrives in 7 milliseconds, Bimal's Success message arrives within 14 milliseconds. So, Amal wins.
To solve this problem, we will follow the following steps:
p := (s * v1) + (2 * t1) q := (s * v2) + (2 * t2) if p is same as q, then: return "Draw" otherwise when p < q, then: return "Amal" Otherwise return "Bimal"
Let us see the below implementation for better understanding −
#include <bits/stdc++.h> using namespace std; string solve(int s, int v1, int v2, int t1, int t2) { int p = (s * v1) + (2 * t1); int q = (s * v2) + (2 * t2); if (p == q) return "Draw"; else if (p < q) return "Amal"; else return "Bimal"; } int main() { int s = 5; int v1 = 1; int v2 = 2; int t1 = 1; int t2 = 2; cout << solve(s, v1, v2, t1, t2) << endl; }
5, 1, 2, 1, 2
Amal
The above is the detailed content of C++ program to find winner of typing game after delay time. For more information, please follow other related articles on the PHP Chinese website!