Acwing 4005. 取石子游戏 [冥冥之中自有天数——博弈论] (暴力打表)

🤠 取石子游戏

在这里插入图片描述
在这里插入图片描述
输入样例

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");
                }               
            }            
        }
    }   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值