
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Winner Name of Stick Crossing Game in C++
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