
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Predict the winner in Coin Game in C++
In this game, there are two players X and Y. our task is to predict who will win the game if both play optimally and X starts the game.
Game
In the coin game, there are two piles with N and M number of coins. One of the players chooses any one of the piles for the game. Then the task is to divide the piles into two halves till any one player cannot further divide the piles.
Let’s take an example to understand the problem,
Input: M = 2 , N = 2 Output:X
Explanation - X starts the game and choose M pile(both are same), and divide the pile into two. Now each will contain only one coin, so Y will be left with no move. This will make X win.
To solve this problem, we need to see the chances that player X wins. The case in which player X wins is when any of the piles will have an even number of coins. Otherwise, Y will be the winner.
Program to show the implementation of our logic
Example
#include <iostream> using namespace std; int isXWinner(int M, int N) { if (M % 2 == 0 || N % 2 == 0) return 1; return 0; } int main() { int M = 1, N = 2; cout<<"Game Starts!\n"; if(isXWinner(M,N)) cout<<"Player X is the Winner"; else cout<<"Player Y is the Winner"; return 0; }
Output
Game Starts! Player X is the Winner
- Related Articles
- Predict the Winner in C++
- Predict the winner of the game on the basis of the absolute difference of sum by selecting numbers in C++
- Program to find winner of stone game in Python
- Program to find winner of array removal game in Python
- Program to find winner of number reducing game in Python
- C++ program to find winner of card game
- Program to find winner of a rower reducing game in Python
- Program to find winner of a rower breaking game in Python
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- C++ Program to find winner of unique bidding game
- Program to find the winner of an array game using Python
- Program to find winner of a set element removal game in Python
- C++ Program to find winner name of stick crossing game
- Find the winner of a game where scores are given as a binary string in Python
