Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import java.util.*
  2.  
  3. val treeMap = TreeMap<Int, String>(sortedMapOf(
  4. 1 to "I",
  5. 4 to "IV",
  6. 5 to "V",
  7. 9 to "IX",
  8. 10 to "X",
  9. 40 to "XL",
  10. 50 to "L",
  11. 90 to "XC",
  12. 100 to "C",
  13. 400 to "CD",
  14. 500 to "D",
  15. 900 to "CM",
  16. 1000 to "M"
  17. )).descendingMap()
  18.  
  19. fun Int.toRoman() : String {
  20. var currentDecimal = this
  21. var roman = StringBuilder()
  22.  
  23. for((d, c) in treeMap){
  24. if(currentDecimal >= d){
  25. val times = currentDecimal / d
  26. val remainder = currentDecimal % d
  27.  
  28. roman.append(c.repeat(times))
  29.  
  30. currentDecimal = remainder
  31. }
  32. }
  33.  
  34. return roman.toString()
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement