# Stone Game One Four [closed]

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 either one stone or four stones. The player who picks the last stone, wins. 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 wins, else print "No".

Constraints:

1<=T<=1000

1<=N<=10000


Sample Input and Output:

3
1
Yes
6
Yes
7
No


Here's my code:

#include<stdio.h>

int solve(int N)
{
int x,y;
int a;                              //checks if alice gets the last stone
int k;                              //iterate
x=N/4;
y=N%4;
if(y==0)                            //check if N in multiple of 4
return x%2;                       //if N is odd multiple of 4 alice wins
else                                //if N is not a multiple of 4
{
a=0;                          //initialize
if(x%2)                       //if N is odd multiple of 4
{
for(k=1;k<y;++k)        //loop to check if alice gets the last stone
a=++k;
if(a==y)
return 1;
else
return 0;
}
else                          //if N is even multiple of 4
{
for(k=0;k<y;++k)        //loop to check if alice gets the last stone
a=++k;
if(a==y)
return 1;
else
return 0;
}
}
}

int main()
{
int T;                              // Test Cases
int N;                              // No of stones
int result;
int i;                              //iterate
scanf("%d",&T);
if(T<1||T>1000)                   //constraints
return 0;
for(i=0;i<T;i++)
{
scanf("%d",&N);
if(N<1||N>10000)                //constraints
return 0;
result=solve(N);
if(result)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}


The contest engine shows that there are 4 tests with Wrong Answers. I think my code is just fine in every aspect but I can't figure out where my code is failing. I'm a novice in competitive programming and I'd grateful for any help to increase my understanding in the problem.

## closed as off-topic by Malachi♦, Heslacher, rolfl♦Aug 13 '14 at 14:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Malachi, Heslacher, rolfl
If this question can be reworded to fit the rules in the help center, please edit the question.

• So, are you getting the right answer, or not? If you're not getting the right answer, this may be off-topic for this site. – RubberDuck Aug 13 '14 at 12:52
• I'm getting the right results as shown in sample i/p and o/p and a few other which I can think of. I think my code generates the right answers for most of the test cases but the contest engine tells that there are 4 tests with wrong answers and I can't figure out which are those tests. – invincible Aug 13 '14 at 13:01
• This question is being discussed on meta. – RubberDuck Aug 13 '14 at 13:26
• what coding challenge site is this from? – Malachi Aug 13 '14 at 13:53
• Note, this code is part of an ongoing, and current competition, I do not believe getting outside help is in the spirit of that contest. Once the contest is over, maybe – rolfl Aug 13 '14 at 18:25

I see one error. a is initialized at the start, and thereafter retains the value it had in the last test. It should be initialized immediately before use.

Aside from that, I'm not sure why some of your answers were wrong. With this structure and without comments, I found it quite difficult to follow the logic. I recommend the following structural changes:

After main() reads each N, it should pass it to a solve(N) function that does the work.

solve(N) tests various cases. Each case should be preceded by a comment explaining how the solution is derived for that case.

The cases should not involve more nested blocks than absolutely necessary. As soon as solve(N) finds an answer, it should return the answer (1 for Yes, 0 for No), and have main() do the printing. That saves print statements, and makes the code shorter in other ways; for example, this code:

if(y==0)
{
if(!(x%2==0))
printf("Yes\n");
else
printf("No\n");
}


becomes:

if (y == 0)
return x % 2;


Note that x % 2 is equivalent to !(x % 2 == 0), and is much easier to read and understand.

Is this code golf or something? It seems excessively brief, at the expense of clarity. It's better to put spaces around operators and keywords, and use indentation consistent with the logic. (One of the many reasons I love Python: it doesn't let you use misleading indentation. The indentation is the logic.)

Your variable declarations should declare one variable per line, with comments saying what they do (or better, meaningful names that don't need comments):

int T;  // number of tests
int i;  // test iterator
int N;  // number of stones
int k;  // what does it do?
int a;  // what does it do?
int x;  // perhaps rename to Ndiv4
int y;  // perhaps rename to Nmod4


The range checks have more parentheses than they need. If they find an input out of range, they should return 1 instead of 0, since that's an error.

Statements like a = ++k; are legal and have well-defined behavior, but they're tricky to read and understand. It's better to break them into two clear statements. It's also better to increment k in the for loop introduction if you can (and I don't see why you can't, in these cases); people are used to reading loops that way, it's an idiom.

• Thanks for the tips about how to write a readable program but my problem is that according to me my code is right but according to the contest engine it produces wrong answers for some cases. Which are those cases? My code doesn't have any syntax or compilation or run time errors. It is just failing a few cases and i want to know which cases they are. – invincible Aug 13 '14 at 14:42
• Does initializing a before each use help? – Tom Zych Aug 13 '14 at 14:54
• @invincible, run unit tests, figure out the answers ahead of time and run them through your application to see if it produces the right answer – Malachi Aug 13 '14 at 14:56
• ok it neither helps nor does it create a problem.Why do you think it is causing a problem? Run my code in any compiler it will give you the desired result – invincible Aug 13 '14 at 14:57
• Except the compiler at the contest engine, apparently. Well, if you add some comments explaining the logic, others will be more able to find errors. – Tom Zych Aug 13 '14 at 14:59