- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在做一些编码挑战,出现了一个问题,大致是这样的:
"Two players each taking turns starting with player one. There are N sticks given, each player takes 1, 2 or 3 sticks on their turn, the player to take the last stick loses, the goal is to find an algorithm that lets player one win with certainty (not always possible, player two is supposed to take turns that will ensure victory) and output 1, 2 or 3 as the starting amount of sticks taken or 0 if it's impossible to win. Input is N. Example: Input:2 Output:1"
我试着考虑一下,但我想到的是它需要检查每一个可能的结果,因为如果 N 很大,所有的可能性都可以链接在一起。我还认为,如果玩家 2 拿走最后一根棍子以免输,那就是玩家 1 拿走 N-1(无论是只拿 N-1 还是拿 N-1、N-2 或 N- 1, N-2, N-3) 把N留给玩家2,只有这样才能确保胜利。原来解决方案是 (N-1) mod 4,但我不明白为什么会这样。
所以我的问题是你如何处理这样的问题,为什么解是模数?也有办法发现像这样的模数问题吗?其他编码员做得相当快,所以我想熟能生巧,但我不知道从哪里开始。
最佳答案
它是模 4 因为如果一个玩家有优势,如果第一个玩家拿了 1 个,他可以拿 3 根棍子,如果第一个玩家拿了 2 个,他拿 2 个,如果第一个玩家拿了 3 个,他可以拿 1 个棍子来保持同样的优势。另一个玩家根本没有任何控制权。
把问题倒过来:
你不必关心一个大N,你只需要分析当只剩下4根或更少的时候情况是什么样的。
当剩下 1、2、3 或 4 根棍子时,谁会获胜?
当剩下 4n+1、4n+2、4n+3 或 4n+4 根棍子时,谁会获胜?
关于algorithm - 接近模数使用的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40752626/
我想创建一个返回值的方法(我们称之为“z”)。它的值由另一个值决定(我们称之为“y”)。基本上我想要的是满足以下条件: 当 x 接近 0 时,z 接近 100。 当 x 接近无穷大时,z 接近 0。
我正在尝试使用 Java 中的PreparedStatement 执行查询。 当我尝试执行查询时,收到错误号 1064(语法错误)。 我已经在 MySQL 查询浏览器中使用替换值对此进行了测试,效果很
我正在开发一个应用程序来解析 Scala 中的命令。命令的一个例子是: todo get milk for friday 所以计划是让一个非常智能的解析器将行分开并识别命令部分以及字符串中有时间引用的
来自 http://directwebremoting.org/dwr/reverse-ajax/index.html ,它表示它支持轮询、 cometd 、搭载。这是否意味着当我们实现这种方法时,我
我开始研究一个概念,该概念要求我找到一种方法,以给定的速度将矩形移向给定的点。我正在为 Android 开发,所以这对速度非常关键(它也将针对可能的数百个对象计算每一帧。) 我能想到的解决方案如下:
我正在处理一个处理“门票”的表(状态=“开放”或状态=“关闭”)。当票证关闭时,相关系统不会更改状态,而是会创建一个具有“已关闭”状态的重复条目。 对于“ticket_number”关键字段,如果存在
我正在尝试在 python 中执行一些 n-gram 计数,我想我可以使用 MySQL(MySQLdb 模块)来组织我的文本数据。 我有一个很大的表,大约有 1000 万条记录,代表由唯一数字 ID(
我正在尝试将数据添加到 mariadb 表中。我想将 val0 到 val5 作为查询的值传递。但我收到错误 OperationalError: close "%": 语法错误代码 list_Valu
我正在使用 (Py)OpenGL 显示 256 色索引图像。我将着色器与包含调色板的一维纹理一起使用。这是片段着色器代码: #version 330 uniform sampler2D texture
对于我的元素 areallybigpage.com (*),我想看看我们能用 CSS 的 transform: scale(...) 走多远。 这有效并以正常大小显示文本: #id1 { positi
我有两列带有数字数据的 Pandas 表(dtype flaot64)。 我将每列四舍五入到小数点后有 2 位数字,然后使用函数将其四舍五入到接近 0.5,但由于某种原因,只有一列四舍五入为 0.05
我正在构建一个由用户登录和注册组成的应用程序,但每次我在模拟器上测试它时,我都会收到强制关闭。以下是我在日志猫中收到的错误: 08-14 14:06:28.853: D/dalvikvm(828):
我正在尝试在 Python 中实现 Strassen 矩阵乘法。我已经让它发挥了一些作用。这是我的代码: a = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]] b
为什么这不起作用?这与 = 附近的命令字符串语法有关,但我似乎无法弄清楚,在线示例似乎完全相同。编辑: Activated In 是一列。 示例来自 How to select data from d
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及
我有一个测试区,它是来自数据库的动态文本,可能有数千个单词。我希望它中断并在每段中用句号将近 100 个(任意长度)单词作为一个段落。我能够在 100 个单词后中断,但不能完全停止。为了在 100 个
我是 hadoop 和 hive 的新手。我正在尝试将数据加载到配置单元表中,但遇到以下错误。 另一方面,我尝试使用语句 stmt.execute("INSERT INTO employee VALU
这是来自一个统计项目。我定义了下面的函数,但是当n接近400时,第二个方法很慢。第一个方法很好(这里有人帮助了我in this question) import Math.Combinatorics.
我正在尝试创建一个 css 侧边菜单,但是当我关闭菜单并将 div 容器宽度设置为 0 时,链接仍然可见。 这是 jsfiddle - https://jsfiddle.net/atLvp6k7/ 有
我对 MySQL 还很陌生。我必须使用输出参数调用存储过程。我在互联网上搜索了很多,但没有找到解决我的问题的正确方法。如果我使用 @outputParamName 调用存储过程,它会说我在 NULL
我是一名优秀的程序员,十分优秀!