Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*
- val treeMap = TreeMap<Int, String>(sortedMapOf(
- 1 to "I",
- 4 to "IV",
- 5 to "V",
- 9 to "IX",
- 10 to "X",
- 40 to "XL",
- 50 to "L",
- 90 to "XC",
- 100 to "C",
- 400 to "CD",
- 500 to "D",
- 900 to "CM",
- 1000 to "M"
- )).descendingMap()
- fun Int.toRoman() : String {
- var currentDecimal = this
- var roman = StringBuilder()
- for((d, c) in treeMap){
- if(currentDecimal >= d){
- val times = currentDecimal / d
- val remainder = currentDecimal % d
- roman.append(c.repeat(times))
- currentDecimal = remainder
- }
- }
- return roman.toString()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement