http://d.hatena.ne.jp/JunichiIto/20111102/1320253815
Scalaで書いてみたけど、Scalaっぽいのかわからない。
object Main { val cols = Range('A'.toInt, 'Z'.toInt + 1, 1).map { n => n.toChar } val stringPattern = "[A-Z]+".r def toNumber(alpha: String, num: Int): Int = { if (alpha.isEmpty) { num } else { toNumber(alpha.drop(1), num * 26 + (cols.indexOf(alpha.head) + 1)) } } def toAlpha(num: Int, alpha: String): String = { if (num <= 0) { alpha } else { (num % 26) match { case 0 => cols(25) + alpha case n: Int => toAlpha(num / 26, cols(n - 1) + alpha) } } } def main(args: Array[String]) { args(0) match { case stringPattern() => println(toNumber(args(0), 0)) case _ => println(toAlpha(args(0).toInt, "")) } } }