2021-12-08:扑克牌中的红桃J和梅花Q找不到了,为了利用剩下的牌做游戏,小明设计了新的游戏规则: 1) A,2,3,4....10,J,Q,K分别对应1到13这些数字,大小王对应0; 2) 游
2021-12-08:扑克牌中的红桃J和梅花Q找不到了,为了利用剩下的牌做游戏,小明设计了新的游戏规则:
- A,2,3,4…10,J,Q,K分别对应1到13这些数字,大小王对应0;
- 游戏人数为2人,轮流从牌堆里摸牌,每次摸到的牌只有“保留”和“使用”两个选项,且当前轮必须做出选择;
- 如果选择“保留”当前牌,那么当前牌的分数加到总分里,并且可以一直持续到游戏结束;
- 如果选择“使用”当前牌,那么当前牌的分数*3,加到总分上去,但是只有当前轮,下一轮,下下轮生效,之后轮效果消失。
- 每一轮总分大的人获胜。
假设小明知道每一轮对手做出选择之后的总分,返回小明在每一轮都赢的情况下,最终的最大分是多少?
如果小明怎么都无法保证每一轮都赢,返回-1。
来自字节跳动。
答案2021-12-08:
递归。
当前来到index位置,牌是cands[index]值。
对手第i轮的得分,sroces[i]。
int hold : i之前保留的牌的总分。
int cur : 当前轮得到的,之前的牌只算上使用的效果,加成是多少。
int next : 之前的牌,对index的下一轮,使用效果加成是多少。
返回值:如果i…最后,不能全赢,返回-1。
如果i…最后,能全赢,返回最后一轮的最大值。
代码用golang编写。代码如下:
package main
import "fmt"
func main() {
cands := []int{1, 3, 4, 5}
sroces := []int{1, 3, 4, 5}
ret := f(cands, sroces, 0, 0, 0, 0)
fmt.Println(ret)
}
func f(cands []int, sroces []int, index, hold, cur, next int) int {
if index == 25 { // 最后一张
all := hold + cur + cands[index]*3
if all <= sroces[index] {
return -1
}
return all
}
// 不仅最后一张
// 保留
all1 := hold + cur + cands[index]
p1 := -1
if all1 > sroces[index] {
p1 = f(cands, sroces, index+1, hold+cands[index], next, 0)
}
// 爆发
all2 := hold + cur + cands[index]*3
p2 := -1
if all2 > sroces[index] {
p2 = f(cands, sroces, index+1, hold, next+cands[index]*3, cands[index]*3)
}
return getMax(p1, p2)
}
// 26 * 341 * 78 * 39 = 2 * (10 ^ 7)
func process(cards, scores []int, index, hold, cur, next int) int {
if index == 25 {
all := hold + cur + cards[index]*3
if all > scores[index] {
return all
} else {
return -1
}
} else {
d1 := hold + cur + cards[index]
p1 := -1
if d1 > scores[index] {
p1 = process(cards, scores, index+1, hold+cards[index], next, 0)
}
d2 := hold + cur + cards[index]*3
p2 := -1
if d2 > scores[index] {
p2 = process(cards, scores, index+1, hold, next+cards[index]*3, cards[index]*3)
}
return getMax(p1, p2)
}
}
// cur -> 牌点数 -> * 3 之后是效果
// next -> 牌点数 -> * 3之后是效果
func p(cands, sroces []int, index, hold, cur, next int) int {
if index == 25 { // 最后一张
all := hold + cur*3 + cands[index]*3
if all <= sroces[index] {
return -1
}
return all
}
// 不仅最后一张
// 保留
all1 := hold + cur*3 + cands[index]
p1 := -1
if all1 > sroces[index] {
p1 = f(cands, sroces, index+1, hold+cands[index], next, 0)
}
// 爆发
all2 := hold + cur*3 + cands[index]*3
p2 := -1
if all2 > sroces[index] {
p2 = f(cands, sroces, index+1, hold, next+cands[index], cands[index])
}
return getMax(p1, p2)
}
func getMax(a, b int) int {
if a > b {
return a
} else {
return b
}
}
2021-12-08:扑克牌中的红桃J和梅花Q找不到了,为了利用剩下的牌做游戏,小明设计了新的游戏规则: 1) A,2,3,4....10,J,Q,K分别对应1到13这些数字,大小王对应0; 2) 游的更多相关文章
- 2021.12.08 P1848 [USACO12OPEN]Bookshelf G(线段树优化DP)
2021.12.08 P1848 [USACO12OPEN]Bookshelf G(线段树优化DP) https://www.luogu.com.cn/problem/P1848 题意: 当农夫约翰闲 ...
- 2021.12.08 [SHOI2009]会场预约(平衡树游码表)
2021.12.08 [SHOI2009]会场预约(平衡树游码表) https://www.luogu.com.cn/problem/P2161 题意: 你需要维护一个 在数轴上的线段 的集合 \(S ...
- 2021.12.08 平衡树——FHQ Treap
2021.12.08 平衡树--FHQ Treap http://www.yhzq-blog.cc/fhqtreapzongjie/ https://www.cnblogs.com/zwfymqz/p ...
- 剑指offer-第六章面试中的各项能力(扑克牌中的顺子)
//扑克牌的顺子 //题目:在一个扑克牌中随机的抽5张牌,看是不是顺子.大小王为0,A为1,J为11,Q为12,K为13.其他数字为自己本身. //思路:大小王可以代表任意一个数字,因此我们在看是不是 ...
- 剑指offer——71扑克牌中的顺子
题目描述 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决 ...
- 【转载】【时序约束学习笔记1】Vivado入门与提高--第12讲 时序分析中的基本概念和术语
时序分析中的基本概念和术语 Basic concept and Terminology of Timing Analysis 原文标题及网址: [时序约束学习笔记1]Vivado入门与提高--第12讲 ...
- 剑指XX游戏(六) - 轻松搞定面试中的红黑树问题
原文地址 http://blog.csdn.net/silangquan/article/details/18655795?utm_source=tuicool&utm_medium=refe ...
- 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...
- 2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组)
2021.12.10 P2516 [HAOI2010]最长公共子序列(动态规划+滚动数组) https://www.luogu.com.cn/problem/P2516 题意: 给定字符串 \(S\) ...
- 今天遇到的面试题for(j=0,i=0;j<6,i<10;j++,i++) { k=i+j; } k 值最后是多少?
for(j=0,i=0;j<6,i<10;j++,i++) { k=i+j; } k 值最后是多少? <script type="text/javascript" ...
随机推荐
- vue树形结构图
1.下载插件:cnpm i vue2-org-tree 2.下载less-loader不然报错(this.getOptions is not a function):npm install less- ...
- Linux命令示例记录-20230313【持续更新中】
1. ip命令 1.1. 摘要 ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具.例如:ifconfig.route等.这个手册将分章节介绍ip命令及其选项. ...
- SpringBoot使用邮件发送
使用场景: 定时任务报错 消息推送 日志报错提醒 1.导入依赖 <dependency> <groupId>org.springframework.boot</group ...
- GUI编程实战--贪吃蛇
GUI编程实战--贪吃蛇 参考:遇见狂神说 https://space.bilibili.com/95256449 界面绘制 帧:如果时间片足够小,就是动画,一秒30帧.连起来是动画,拆开是静态的图片 ...
- Salesforce LWC学习(十二) Dependence Picklist实现
本篇可参看: Salesforce LWC学习(六) @salesforce & lightning/ui*Api Reference salesforce零基础学习(八十七)Apex 中Pi ...
- solidity中的mapping
mapping可以理解为python中对字典的键值遍历,键是唯一的而值是可以重复的 mapping函数的构造: mapping(_KeyType => _ValueType) mapping ...
- MySQL四种日志binlog/redolog/relaylog/undolog
优质博文:IT-BLOG-CN 一.binlog binlog记录数据库表结构和表数据变更,比如update/delete/insert/truncate/create,它不会记录select.存储着 ...
- [J2EE:中间件]LOG4J及配置文件(log4j.properties)详解
1 简介 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下 ...
- odoo 开发入门教程系列-继承(Inheritance)
继承(Inheritance) Odoo的一个强大方面是它的模块化.模块专用于业务需求,但模块也可以相互交互.这对于扩展现有模块的功能非常有用.例如,在我们的房地产场景中,我们希望在常规用户视图中直接 ...
- openwrt开发使用-arping
前言 IP冲突引起的网络异常,可以通过检查IP是否冲突,排除故障.我们可以用一些工具进行检查,例如arp-scan.arping软件进行查看. 这里使用arping进行检查设备的MAC地址,通过查查看 ...