If your input is 2 2 1
, then b
and c
will both be 1, making f
0 and g
1 in the first set of conditionals. h
will be (2 * 1) % 4
or 2, making an update to f
(0 + 1 = 1). Since g
is 1, g-h
is -1, which will lead to you executing f=f+(g/4)+1
which is f=1 + (-1/4)+1
which is 1 + 0 + 1 = 2 in integer math.
I think you wanted to check if g-h>0
instead of g!=-1
, but there are a ton of places you could simplify your code. Note that using a debugger and stepping through this would have shown you where your problems are much faster, and be much more helpful to increasing your skills, than asking SO.
Just for anyone else looking at this question, this is a fairly simple answer to the problem.