Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-08-27 16:29:06
forward
771 people have browsed it

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.

Steps

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"
Copy after login

Example

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;
}
Copy after login

Input

5, 1, 2, 1, 2
Copy after login

Output

Amal
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template