小编Dom*_*ick的帖子

解决约瑟夫斯置换问题

假设100个人以1到100的顺序站成一个圆圈.1号有一把剑.他杀死了下一个人(即第2号)并将剑交给下一个人(即第3号).所有人都这样做,直到只有1人幸存.最后一个幸存的是哪个号码?

有100人从1到100.

我试过了

persons <- c(1:100)
for (i in 1:100) {
  qu <- persons[seq_along(persons) %% 2 > 0]
  if (q2 == 2) {
    print(q2[2])
  }
}
Run Code Online (Sandbox Code Playgroud)

还有这个

q=0
 while(q==0){ persons=persons[seq_along(persons) %% 2 > 0]
 if(length(persons)==2){ print(persons[2])
 q=1}}
Run Code Online (Sandbox Code Playgroud)

但这给出了答案,因为65这是错误的,并没有解决目的.

如何做到这一点的任何解决方案?

for-loop r

14
推荐指数
2
解决办法
1261
查看次数

从整个字符串JavaScript中提取两个单词之间的文本

我有一个字符串,我想提取两个文本之间的所有单词,这样:

var str="This is the number I want +2143334 !, again this is the next number I want +234343443 !, last number I want +76645 !, fininshed";

var ext = str.split('want').pop().split('!,').shift(); 
alert(ext);
Run Code Online (Sandbox Code Playgroud)

但这只是给出了+2143334.我想要的是三个匹配,即:

+2143334, +234343443, +76645
Run Code Online (Sandbox Code Playgroud)

怎么做到呢?

javascript regex string-matching

0
推荐指数
1
解决办法
928
查看次数

标签 统计

for-loop ×1

javascript ×1

r ×1

regex ×1

string-matching ×1