Answer on Question #53512, Programming / C++
Problem.
Alice and Bob are playing a game called "Stone Game". Stone game is a two-player game. Let N be the total number of stones. In each turn, a player can remove 1, 2 or 3 stones. The player who picks the last stone, loses. They follow the "Ladies First" norm. Hence Alice is always the one to make the first move. Your task is to find out whether Alice can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of stones.
Output Format:
Print "Yes" in the case Alice can win, else prints "No".
Constraints:
1<=T<=10
1<=N<=10000
Sample Input and Output
SNo. Input Output
1
2
1
3
No
Yes
#include <iostream>
using namespace std;
int main() {
int T, N;
// Reading the number of inputs
cin >> T;
bool arr[T];
// Reading and checking number of stones
for (int i = 0; i < T; i++) {
cin >> N;
// If number of stones is divisible by 4, then Bob has winning strategy
if (N % 4 == 0) {
arr[i] = false;
} else {
// If number of stones isn't divisible by 4, then Alice has winning strategy
arr[i] = true;
}
}
// Output
for (int i = 0; i < T; i++) {
if (arr[i]) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}Output.
2
3
6
Yes
Yes
https://www.AssignmentExpert.com
Comments