i have no idea, as to how to do it and if i don't i will fail the subject and remain in the same year. |
But here's the generic approach to solving such problems. Mostly I'm right on this one but keep in mind that I'm a noob. So you have a pile of N coins. Alice and Bob take coins from the pile. When Alice and Bob take coins from the pile, we are basically subtracting from the pile. Suppose Alice just finished taking 4 coins from the pile leaving 2 coins, and Bob needs to take 4 coins. 2-4 would give -2 which is not possible, which I think you understood as well. So how about something like this? (pseudo code)
Hopefully if there are any improvements, somebody else can mention it. I'm sure you will understand most of the code. By the way we're using a for-loop over a while loop because we can use "i" the iterating variable, we could have also used a while loop it's the same thing. Okay mostly you probably could understand everything but you might get confused in one detail - why we're assigning the winner to be the person who played last turn. If you do then I think you should think about it yourself ;^), that's how you learn and that's how you build your logic! |
there was no decision there, but here you need to figure out the optimal play. |
Each player in turn takes from one to K pebbles from the box. |
pow(k, turn)
keskiverto wrote: |
---|
We know that Ben can take 1, 2, or 3 on the first turn. That is a decision. You have to compute the outcome of each choice in order to know which (if any) are optimal and lead to victory. |