失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > -12-08:扑克牌中的红桃J和梅花Q找不到了 为了利用剩下的牌做游戏 小明设计了新

-12-08:扑克牌中的红桃J和梅花Q找不到了 为了利用剩下的牌做游戏 小明设计了新

时间:2020-12-15 07:08:48

相关推荐

-12-08:扑克牌中的红桃J和梅花Q找不到了 为了利用剩下的牌做游戏 小明设计了新

-12-08:扑克牌中的红桃J和梅花Q找不到了,为了利用剩下的牌做游戏,小明设计了新的游戏规则:

A,2,3,4…10,J,Q,K分别对应1到13这些数字,大小王对应0;游戏人数为2人,轮流从牌堆里摸牌,每次摸到的牌只有“保留”和“使用”两个选项,且当前轮必须做出选择;如果选择“保留”当前牌,那么当前牌的分数加到总分里,并且可以一直持续到游戏结束;如果选择“使用”当前牌,那么当前牌的分数*3,加到总分上去,但是只有当前轮,下一轮,下下轮生效,之后轮效果消失。每一轮总分大的人获胜。

假设小明知道每一轮对手做出选择之后的总分,返回小明在每一轮都赢的情况下,最终的最大分是多少?

如果小明怎么都无法保证每一轮都赢,返回-1。

来自字节跳动。

答案-12-08:

递归。

当前来到index位置,牌是cands[index]值。

对手第i轮的得分,sroces[i]。

int hold : i之前保留的牌的总分。

int cur : 当前轮得到的,之前的牌只算上使用的效果,加成是多少。

int next : 之前的牌,对index的下一轮,使用效果加成是多少。

返回值:如果i…最后,不能全赢,返回-1。

如果i…最后,能全赢,返回最后一轮的最大值。

代码用golang编写。代码如下:

package mainimport "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]*3if all <= sroces[index] {return -1}return all}// 不仅最后一张// 保留all1 := hold + cur + cands[index]p1 := -1if all1 > sroces[index] {p1 = f(cands, sroces, index+1, hold+cands[index], next, 0)}// 爆发all2 := hold + cur + cands[index]*3p2 := -1if 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]*3if all > scores[index] {return all} else {return -1}} else {d1 := hold + cur + cards[index]p1 := -1if d1 > scores[index] {p1 = process(cards, scores, index+1, hold+cards[index], next, 0)}d2 := hold + cur + cards[index]*3p2 := -1if 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]*3if all <= sroces[index] {return -1}return all}// 不仅最后一张// 保留all1 := hold + cur*3 + cands[index]p1 := -1if all1 > sroces[index] {p1 = f(cands, sroces, index+1, hold+cands[index], next, 0)}// 爆发all2 := hold + cur*3 + cands[index]*3p2 := -1if 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}}

左神java代码

-12-08:扑克牌中的红桃J和梅花Q找不到了 为了利用剩下的牌做游戏 小明设计了新的游戏规则: 1) A 2 3 4....10 J Q K分别对应1到13这些数字 大小王对应0; 2) 游

如果觉得《-12-08:扑克牌中的红桃J和梅花Q找不到了 为了利用剩下的牌做游戏 小明设计了新》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。