Saturday, 15 August 2015

TCS Codevita : Stone Game - Remove Last

Problem : Stone Game - Remove Last
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



Solution in C++ :
#include <iostream>
using namespace std;

int main() {
int nn, j,aa;
  cin >> aa;
while(aa--)
{
cin >> nn;
int st[nn+1];
for(j = 0; j <= nn; j++)
st[j] = 0;
st[1] = 0;
st[2] = 1;
st[3] = 1;
for(j = 4; j <= nn; j++)
{
if(st[j-1] == 0 || st[j-2] == 0 || st[j-3]==0)
st[j] = 1;
else
st[j] = 0;
}
if(st[nn] == 1)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}


No comments:

Post a Comment