C++ program to find winner of typing game after delay timing

C++Server Side ProgrammingProgramming

Suppose we have five numbers s, v1, v2, t1 and t2. Amal and Bimal are playing a typing game, they are playing their game online. In this game they will type a string whose length is s. Amal types one character in v1 milliseconds and Bimal types one 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 competition passes for a participant as follows −

  • Exactly after t milliseconds after the game start the participant receives the text to be entered.

  • Right after that he starts to type it.

  • Exactly t milliseconds after he ends typing all the text, the site receives information about it.

Who completes faster, will be the winner. If the time for both participants are same, then it is a draw. We have to find the winner.

So, if the input is like s = 5; v1 = 1; v2 = 2; t1 = 1; t2 = 2, then the output will be Amal, because information on the success of Amal comes in 7 milliseconds, of Bimal in 14 milliseconds. So, Amal wins.

Steps

To solve this, we will follow these 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"

Example

Let us see the following implementation to get 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;
}

Input

5, 1, 2, 1, 2

Output

Amal
raja
Updated on 03-Mar-2022 11:20:12

Advertisements