大纲
- 题目
- 地址
- 内容
- 解题
- 代码地址
题目
地址
https://leetcode.com/problems/find-the-winning-player-in-coin-game/description/
内容
You are given two positive integers x and y, denoting the number of coins with values 75 and 10 respectively.
Alice and Bob are playing a game. Each turn, starting with Alice, the player must pick up coins with a total value 115. If the player is unable to do so, they lose the game.
Return the name of the player who wins the game if both players play optimally.
Example 1:
Input: x = 2, y = 7
Output: “Alice”
Explanation:
The game ends in a single turn:
Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.
Example 2:
Input: x = 4, y = 11
Output: “Bob”
Explanation:
The game ends in 2 turns:
Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.
Bob picks 1 coin with a value of 75 and 4 coins with a value of 10.
Constraints:
- 1 <= x, y <= 100
解题
这题就是要求每次从(x,y)上减去(1,4),求最多能减少几次,并要保证最后x,y都要大于等于0?然后我们可以通过次数来判断最终是Alice是最后一次,还是Bob是最后一次。
#include <string>
using namespace std;class Solution {
public:string losingPlayer(int x, int y) {return min(x, y / 4) % 2 == 1 ? "Alice" : "Bob";}
};
代码地址
https://github.com/f304646673/leetcode/tree/main/3222-Find-the-Winning-Player-in-Coin-Game/cplusplus