二十一点王牌序列

问题描述 投票:3回答:1

我有一个序列说:11,11,6,4,11,10,6,......它模拟了玩家在二十一点的手牌的以下可能的牌值。

当这些数字的总和累积到11或更大时,我试图使这个点之后的每个值11等于1。

累积金额为:11,22,28,32,......

期望的结果:11,1,6,1,1,10,6,......

以下是我尝试过的失败:

nphand = c(11,11,6,4,11,10,6)
v=cumsum(nphand)
p=v[v<=11]

for (i in (length(p)+1):length(nphand)){
    if (nphand[i]==11){
        nphand[i]==1
    }
}

任何帮助和/或建议将不胜感激。

r sequence
1个回答
2
投票

这应该工作。

nphand = c(11,11,6,4,11,10,6)
v=cumsum(nphand)
p=v[v<=11]

for (i in 1:length(nphand)){
  cards <- nphand[1:i]
  elevens <- cards[-1] %in% 11
  if(sum(cards)>=11 &  sum(elevens) >=1){
    cards[which(elevens) +1] <- 1
  }
  nphand[1:i] <- cards
}



 > nphand
    [1] 11  1  6  4  1 10  6

看起来这取决于第一张卡是11.下面的解决方案应该工作,无论如何:

nphand = c(2,11,6,4,11,10,6)
v=cumsum(nphand)
p=v[v<=11]

for (i in 1:length(nphand)){
  cards <- nphand[1:i]
  elevens <- cards %in% 11
  if(sum(cards)>=11 &  sum(elevens) >=1){
    cards[which(elevens[-1]) + 1] <- 1
  }
  nphand[1:i] <- cards
}
© www.soinside.com 2019 - 2024. All rights reserved.