
- 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
Even-odd turn game with two integers in C++
In this problem, we are given three integer values A , B and T. Our task is to create a program to play Even-odd turn game with two integers.
The two integer value are :
T, that denotes the number of turns in the game.
A denotes the value for player1
B denotes the value for player2
If the value of T is odd, the value of A is multiplied by 2.
If the value of T is even, the value of B is multiplied by 2.
We need to find and return value of max(A, B) / min(A, B) at the end.
Let’s take an example to understand the problem,
Input: A = 3, B = 4, T = 3
Output: 1
Explanation:
1st Turn : T is odd, A is multiplied by 2, A = 6.
2nd Turn: T is even, B is multiplied by 2, B = 8.
3rd Turn: T is odd, A is multiplied by 2, A = 12.
A = 12 B = 4
max(A, B) = max(12, 4) = 12
min(A, B) = min(12, 4) = 4
max(A, B) / min(A, B) = 12/ 8 = 1
Solution Approach:
A simple solution to the problem would be calculating the value of A and B after T turns and then return the value of max(A, B) / min(A, B). This is an effective solution by taking T iterations.
But their can be a more effective solution based on the fact that for even value of T, the value of new A is N*A and the value of new B is N*B.
This make the value of max(A, B) / min(A, B) a constant which is equal to
max(A, B) / min(A, B).
If the value of T is odd, the value of A will be 2*N*A and the value of B is N*B.
This makes the value of max(A, B) / min(A, B) a constant which is equal to max(2A, B) / min(2A, B).
The result of the problem max(A, B) / min(A, B) =
max(A, B) / min(A, B), if T is even
max(A, B) / min(A, B), if T is odd
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int EvenOddGame(int A, int B, int T) { if ( T%2 == 0) return (max(A, B) / min(A, B)); else return (max(2*A, B) / min(2*A, B)); return -1; } int main() { int A = 3, B = 2, T = 3; cout<<"The return value of even odd game is "<<EvenOddGame(A, B, T); }
Output −
The return value of even odd game is 3
- Related Articles
- C# program to split the Even and Odd integers into different arrays
- Matching odd even indices with values in JavaScript
- The sum of two prime numbers $(>2)$ is(A) odd (B) even (C) prime (D) even or odd
- Even numbers at even index and odd numbers at odd index in C++
- Separate Odd and Even Elements into Two Separate Arrays in Java
- Odd Even Jump in C++
- Count subarrays with same even and odd elements in C++
- Number of integers with odd number of set bits in C++
- Odd Even Linked List in Python
- Separate odd and even in JavaScript
- Replace Odd Numbers with Square root & Even Numbers with Square in Java
- Odd even index difference - JavaScript
- Find two consecutive odd positive integers, sum of whose squares is 970.
- Count number of ordered pairs with Even and Odd Product in C++
- Count number of ordered pairs with Even and Odd Sums in C++
