
- 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
C++ code to find minimum number starting from n in a game
Suppose we have a number n. In a game initially the value of n is v and the player is able to do the following operation zero or more times: Select a positive integer x that x < n and x is not a divisor of n, then subtract x from n. The goal of the player is to minimize the value of n in the end.
So, if the input is like n = 8, then the output will be 1, because the player can select x = 3 in the first turn, then n becomes 5. We can then choose x = 4 in the second turn to get n = 1 as the result.
Steps
To solve this, we will follow these steps −
if n is same as 2, then: return 2 return 1
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n){ if (n == 2){ return 2; } return 1; } int main(){ int n = 8; cout << solve(n) << endl; }
Input
8
Output
1
- Related Articles
- C++ code to find out who won an n-round game
- C++ code to find final number after min max removal game
- C++ code to find minimum different digits to represent n
- Find the minimum number of steps to reach M from N in C++
- C++ code to find minimum correct string from given binary string
- C++ code to find minimum k to get more votes from students
- C++ code to find minimum arithmetic mean deviation
- Program to find minimum number of groups in communication towers in C++?\n
- C++ code to find minimum difference between concerts durations
- C++ code to find minimum stones after all operations
- C++ code to count number of weight splits of a number n
- Minimum Players required to win the game in C++
- A Number Link Game in C/C++?
- C++ code to find minimum operations to make numbers c and d
- Convert a number m to n using minimum number of given operations in C++

Advertisements