fork download
  1. printf <- function(...) cat(sprintf(...))
  2.  
  3. ithPermutation <- function(n, i)
  4. {
  5. i <- i - 1
  6. r <- rev(sapply(1:n, function(x) {j <- i %% x; i <<- i %/% x; j + 1}))
  7.  
  8. a <- 1:n
  9. sapply(r, function(x) {y <- a[x]; a <<- a[-x]; y})
  10. }
  11.  
  12. for (n in c(1, 2, 3, 123456, 234567, 362880)) {
  13. printf("入力: %s\n", n)
  14. printf("出力: [%s]\n\n", toString(ithPermutation(9, n)))
  15. }
Success #stdin #stdout 0.36s 41088KB
stdin
Standard input is empty
stdout
入力: 1
出力: [1, 2, 3, 4, 5, 6, 7, 8, 9]

入力: 2
出力: [1, 2, 3, 4, 5, 6, 7, 9, 8]

入力: 3
出力: [1, 2, 3, 4, 5, 6, 8, 7, 9]

入力: 123456
出力: [4, 1, 6, 5, 8, 9, 7, 3, 2]

入力: 234567
出力: [6, 8, 4, 7, 5, 3, 2, 1, 9]

入力: 362880
出力: [9, 8, 7, 6, 5, 4, 3, 2, 1]