|
@@ -1,6 +1,12 @@
|
|
|
package main
|
|
|
|
|
|
-var mRoman = map[int]rune{
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "fmt"
|
|
|
+ "math"
|
|
|
+)
|
|
|
+
|
|
|
+var mRoman = map[float64]rune{
|
|
|
1: 'I',
|
|
|
5: 'V',
|
|
|
10: 'X',
|
|
@@ -10,11 +16,64 @@ var mRoman = map[int]rune{
|
|
|
1000: 'M',
|
|
|
}
|
|
|
|
|
|
-func intToRoman(num int) string {
|
|
|
+func digitToRoman(pos, digit int) []rune {
|
|
|
+ res := make([]rune, 0)
|
|
|
+ base := math.Pow10(pos)
|
|
|
+ switch digit {
|
|
|
+ case 0:
|
|
|
+ case 1:
|
|
|
+ fallthrough
|
|
|
+ case 2:
|
|
|
+ fallthrough
|
|
|
+ case 3:
|
|
|
+ for i := 0; i < digit; i++ {
|
|
|
+ res = append(res, mRoman[base])
|
|
|
+ }
|
|
|
+ case 4:
|
|
|
+ res = append(res, mRoman[base], mRoman[base*5])
|
|
|
+ case 5:
|
|
|
+ fallthrough
|
|
|
+ case 6:
|
|
|
+ fallthrough
|
|
|
+ case 7:
|
|
|
+ fallthrough
|
|
|
+ case 8:
|
|
|
+ res = append(res, mRoman[base*5])
|
|
|
+ for i := 5; i < digit; i++ {
|
|
|
+ res = append(res, mRoman[base])
|
|
|
+ }
|
|
|
+ case 9:
|
|
|
+ res = append(res, mRoman[base], mRoman[base*10])
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func intToRomanOld(num int) string {
|
|
|
res := make([]rune, 0)
|
|
|
+ for i := 0; num != 0; i, num = i+1, num/10 {
|
|
|
+ roman := digitToRoman(i, num%10)
|
|
|
+ res = append(roman, res...)
|
|
|
+ }
|
|
|
return string(res)
|
|
|
}
|
|
|
|
|
|
-func main() {
|
|
|
+var roman = [4][10]string{
|
|
|
+ {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
|
|
|
+ {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
|
|
|
+ {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
|
|
|
+ {"", "M", "MM", "MMM"},
|
|
|
+}
|
|
|
|
|
|
+func intToRoman(num int) string {
|
|
|
+ var res bytes.Buffer
|
|
|
+ res.WriteString(roman[3][num/1000])
|
|
|
+ res.WriteString(roman[2][num%1000/100])
|
|
|
+ res.WriteString(roman[1][num%100/10])
|
|
|
+ res.WriteString(roman[0][num%10])
|
|
|
+ return res.String()
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ fmt.Println(intToRoman(1998))
|
|
|
}
|