fork download
  1. if (!require(gtools)) {
  2. install.packages("gtools")
  3. require(gtools)
  4. }
  5.  
  6. OrderMaxMinDiff <- function(n)
  7. {
  8. p <- apply(permutations(n - 2, n - 2), 1, function(v) c(0, v, n - 1))
  9. MinDiff <- apply(t(abs(diff((p)))), 1, min)
  10. MaxMinDiff <- max(MinDiff)
  11. i <- which(MinDiff == MaxMinDiff)
  12. apply(t(p[, i]), 1, function(v) cat(v, sep = ", ", fill = TRUE))
  13. cat(sprintf("最小隣接差が最大値%dになる並べ方は以上の%d通りです。\n", MaxMinDiff, length(i)))
  14. }
  15.  
  16. OrderMaxMinDiff(10)
Success #stdin #stdout #stderr 1.41s 69860KB
stdin
Standard input is empty
stdout
0, 4, 8, 3, 7, 2, 6, 1, 5, 9
0, 5, 1, 6, 2, 7, 3, 8, 4, 9
最小隣接差が最大値4になる並べ方は以上の2通りです。
stderr
Loading required package: gtools