C++ Program to find winner name of stick crossing game

C++Server Side ProgrammingProgramming

Suppose we have two numbers n and k. Amal and Bimal are playing a game. The rules are simple. Amal draws n sticks in a row. After that the players take turns crossing out exactly k sticks from left or right in each turn. Amal starts the game. If there are less than k sticks on the paper before some turn, the game ends. Amal wins if he makes strictly more moves than Bimal. We have to find who will be the winner.

So, if the input is like n = 10; k = 4, then the output will be Bimal. Because Amal crosses out 4 sticks, then Bimal crosses out 4 sticks, and after that there are only 2 sticks left. Amal can't make a move. The players make equal number of moves, so Amal doesn't win.

Steps

To solve this, we will follow these steps −

if floor of (n / k) is even, then:
   return "Amal"
return "Bimal"

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

string solve(int n, int k) {
   if ((n / k) % 2 != 0) {
      return "Amal";
   }
   return "Bimal";
}
int main() {
   int n = 10;
   int k = 4;
   cout << solve(n, k) << endl;
}

Input

10, 4

Output

Bimal
raja
Updated on 04-Mar-2022 06:46:42

Advertisements