R 21点ACE序列

R 21点ACE序列,r,sequence,R,Sequence,我有一个序列说:11,11,6,4,11,10,6,。。。它模拟了21点游戏中玩家手牌的以下可能牌值 当这些数字的总和累积到11或更大时,我试图使在这一点之后出现的11的每个值等于1 其中,累计总和为:11、22、28、32 期望结果:11,1,6,4,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))

我有一个序列说:11,11,6,4,11,10,6,。。。它模拟了21点游戏中玩家手牌的以下可能牌值

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

其中,累计总和为:11、22、28、32

期望结果:11,1,6,4,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
    }
}
nphand=c(11,11,6,4,11,10,6)
v=总和(nphand)
这应该行得通

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
nphand=c(11,11,6,4,11,10,6)
v=总和(nphand)

你的代码运行得很好。你需要改变的唯一一件事是在第二部分将双等号改为单等号。我的意思是使用nphand[I]=1而不是nphand[I]=1。
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
}