🤠 取石子游戏
输入样例
4
0 3
3 3
3 4
4 4
输出样例
Bob
Alice
Bob
Alice
😋 思路如下,懂不懂看造化吧
⭐ 看不懂?无所谓,暴力打表会出手
⭐ 打 100 项找规律,看不出来就认了吧
import java.util.Scanner;
public class 取石子游戏_打表法
{
static int N = 110;
static int f[] = new int[N];//全局数组默认初始化为 0,1 表示先手必胜态
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] d = { 1, 2, k };//状态转移量数组
f[0] = 0;
for (int i = 1; i < n; i++)
{
for (int j = 0; j < 3; j++)
if (i >= d[j] && f[i - d[j]] == 0) // 只要能转到一个 必败态,那当前状态就是必胜态
f[i] = 1;
}
for (int i = 0; i < n; i++)
{
// 适当换行
if (i % 20 == 0)
System.out.println();
System.out.print(f[i]);
}
}
}
⭐ 代码很简单
⭐ 时间复杂度 O(1)
import java.util.*;
class Main{
public static void main(String[] q)
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T -- > 0)
{
int n = sc.nextInt();
int k = sc.nextInt();
if(k % 3 != 0)
{
if(n % 3 != 0)
System.out.println("Alice");
else
System.out.println("Bob");
}
else
{
n %= k + 1;
if(n == k || n % 3 != 0)
{
System.out.println("Alice");
}
else{
System.out.println("Bob");
}
}
}
}
}