Closed ninehills closed 7 years ago
https://leetcode.com/problems/integer-to-roman/#/description
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
https://en.wikipedia.org/wiki/Roman_numerals
罗马计数法看起来复杂,其实可以和十进制有个一一对应关系
package main import "fmt" // ---------------------- func intToRoman(num int) string { r1 := []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"} r2 := []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"} r3 := []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"} r4 := []string{"", "M", "MM", "MMM"} return fmt.Sprintf("%v%v%v%v", r4[num/1000], r3[num%1000/100], r2[num%100/10], r1[num%10]) } // ---------------------- func main() { fmt.Println(intToRoman(90)) fmt.Println(intToRoman(131)) fmt.Println(intToRoman(13)) }
20170721
问题
https://leetcode.com/problems/integer-to-roman/#/description
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
https://en.wikipedia.org/wiki/Roman_numerals
思路
罗马计数法看起来复杂,其实可以和十进制有个一一对应关系
解决