有任意种水果,每种水果个数也是任意的,两人轮流从中取出水果,规则如下: 1)每一次应取走至少一个水果;每一次只能取走一种水果的一个或者全部 2)如果谁取到最后一个水果就胜给定水果种类N和每种水果的个数M1,M2,…Mn,算出谁取胜,编程实现之。
题目的隐含条件是两个人足够聪明,聪明到为了取胜尽可能利用规则。
以上是题目的全部内容,我在考场上简单分析了下决定用递归,但是没想明白。
我的思路和当时的代码
问题转换为谁拿倒数第二种水果的最后一个的问题,继而想到了递归
//返回0表示第一个人赢,返回1表示第二个人赢
//问题归结为,谁拿倒数第二种最后一个苹果谁输
//fruitnum水果种类,a[]对应每种水果个数
int whowins(int fruitnum,int a[])
{
if(fruitnum==1)
return 0;
else
{
考虑水果个数的奇偶性等问题。
}
}
没想太明白这题,希望懂的不吝赐教
Written one using
Java
: https://github.com/terry83299387/MyTest/blob/master/WouldIWinTheGame.javaLet me briefly talk about my thoughts:
Note: This recursive method can ensure that both parties participating in the game are smart enough, because after
玩家A
takes away one or a kind of fruit, he will also try his best to try every possible way when it is玩家B
’s turn. Win by yourself, and declare defeat only when all possibilities fail to win.After thinking carefully again, I found that this question can be calculated very simply:
If there are an odd number of fruit types, we will definitely win
When the number of fruit types is an even number, if the total number of fruits is an odd number, our side wins, otherwise the opponent wins
I took some time to prove the above conclusion:
m=1 must win
m=2 Because no one dares to take the first to finish a fruit, so both parties can only take it one by one. Therefore, an odd number wins
m=3 must win
m=4 No one dares to finish a fruit first, so both sides can only take it one by one. In other words, the "total -4" must be an odd number, so that the other party can enter the final A losing situation, so the total number must be an odd number
Assume k>=3 and k is an odd number, and the above conclusion holds true when m=k and m=k+1.
At this point, the above conclusion is proved to be true by induction. (Actually, the two situations 3 and 4 can be omitted, and the final conclusion can be summarized directly from 1 and 2. But adding 3 and 4 will make it clearer)
I searched online and found this conclusion. Personally, I think this conclusion is incorrect. I will have time to reason again after the interview in the afternoon. If there are any mistakes, please correct me. This conclusion provides an idea for analyzing the problem. I first analyzed three kinds of fruits. From the current analysis, it is definitely inevitable to use recursion, because the problem of three fruits is converted into a problem of finding two fruits; the problem of two fruits is converted into a problem of finding two fruits. A fruit problem, dynamic programming?
Suppose two people are A (take first) and B (take last), A takes the fruit first. Remember the total number of fruits is M (i.e. M1 + M2 + ... + Mn) . Start discussing by situation:
(1) There is 1 kind of fruit
A must win
(2) There are 2 kinds of fruits
At this time, neither person dares to take away all one kind of fruit, because That will send the other party into the must-win state of (1), and you will lose.
So both people can only take it one by one, so who takes the last one is determined by the parity of M.
If M is Odd numbers (definitely one odd number and one even number), A must win; (whoever gets it first wins)
If M is an even number (both are even numbers, both are odd numbers)
If both are even numbers Then A wins, if both are odd numbers, B wins.
** Regarding this point, you may say that what I said is wrong. For example:
If there are 3 fruits of the first type and 2 fruits of the second type, the total number of fruits is an odd number, and the condition is met. If A takes the first fruit first, B takes another, A takes another, then B takes all the second fruit, and B wins.
If you think so, maybe A is not smart enough. If A is smart enough, why not take the even number of fruits, so that A wins. **
(3) There are 3 kinds of fruits
A takes first, he has enough initiative to make the result go in his own favor.
If M is an odd number, it means that at least one kind of fruit has an odd number, all After taking away this kind of fruit, therefore, the total number of fruits is still an even number, which is equivalent to there being two kinds of fruits, and the total number is an even number, which changes to the second situation in (2).
If M is an even number, since N is 3, there is an even number of at least one kind of fruit. After all this kind of fruit is taken away, therefore, there is an even number of fruits in total, which is equivalent to having two kinds of fruits. An even number is also converted into the second case in (2);
A brief analysis, there may be logical errors.
1, take at least one. 2. You can only take one or all of one kind of fruit at a time
Suppose 1: There is only one kind of fruit and N=1, obviously P1 (person 1st) wins. Take it all in one go
If 2: N=2, it is converted to M1, and M2 can only be one of them (-1) at a time.
If 3: N>2, P1 must ensure that the difference between the two remaining fruits is an even number. It is to ensure that the remaining fruits are all odd numbers or all even numbers.
Now, the problem should be clear.
Stop looking for written test questions like this, there are just a few at a time, and they are gone before you get the pleasure. Go install the "Written Test Collection" which contains 90% of the written test questions on the Internet http://bishi.jisupeixun.com