Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
8
Task/Josephus-problem/R/josephus-problem-1.r
Normal file
8
Task/Josephus-problem/R/josephus-problem-1.r
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
jose <-function(s, r,n){
|
||||
y <- 0:(r-1)
|
||||
for (i in (r+1):n)
|
||||
y <- (y + s) %% i
|
||||
return(y)
|
||||
}
|
||||
> jose(3,1,41) # r is the number of remained prisoner.
|
||||
[1] 30
|
||||
21
Task/Josephus-problem/R/josephus-problem-2.r
Normal file
21
Task/Josephus-problem/R/josephus-problem-2.r
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
josephusProblem <- function(n, k, m)
|
||||
{
|
||||
prisoners <- 0:(n - 1)
|
||||
exPos <- countToK <- 1
|
||||
dead <- integer(0)
|
||||
while(length(prisoners) > m)
|
||||
{
|
||||
if(countToK == k)
|
||||
{
|
||||
dead <- c(dead, prisoners[exPos])
|
||||
prisoners <- prisoners[-exPos]
|
||||
exPos <- exPos - 1
|
||||
}
|
||||
exPos <- exPos + 1
|
||||
countToK <- countToK + 1
|
||||
if(exPos > length(prisoners)) exPos <- 1
|
||||
if(countToK > k) countToK <- 1
|
||||
}
|
||||
print(paste0("Execution order: ", paste0(dead, collapse = ", "), "."))
|
||||
paste0("Survivors: ", paste0(prisoners, collapse = ", "), ".")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue