Question:
There are two players, (you and your friend), and you are going to pick up coins alternatively from a bag of coins(total number is n). The rule is every time you or your friend can pick up 1 to 4 coins, and the winner will be the one who picks up the last coin. Then how can you know or what kind of strategy are you going to take to win? Suppose you will pick up first and your friend is as smart as you.
Solution:
Start from small samples:
if there is only 1 coin, you will win;
2 win;
3 win;
4 win;
5 coins, you will lose no matter what you do...
6 , if you choose 1, then leave 5 coins to you friend to choose from, and you will will.
7, you choose 2, and then win.
....
So if the total number of coins are n, then if n%(1+4) == 0, you will never win, otherwise you can win if you and your friend are clever enough.
Further thinkings:
1: how about that you (and your friend) can pick up up to 10 coins, instead of 4?
2: how about that each number can be only chosen once? see the following post:
http://startleetcode.blogspot.com/2017/01/464-can-i-win.html
No comments :
Post a Comment