Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
把整型数字转化为罗马数字。提前用一个字典表存储好所有对应关系即可
public class Solution { public string IntToRoman(int num) { string result = ""; Dictionary<int, string> romaDic = new Dictionary<int, string>(); romaDic.Add(1000, "M"); romaDic.Add(900, "CM"); romaDic.Add(500, "D"); romaDic.Add(400, "CD"); romaDic.Add(100, "C"); romaDic.Add(90, "XC"); romaDic.Add(50, "L"); romaDic.Add(40, "XL"); romaDic.Add(10, "X"); romaDic.Add(9, "IX"); romaDic.Add(5, "V"); romaDic.Add(4, "IV"); romaDic.Add(1, "I"); foreach (var item in romaDic) { int repeatCount = num / item.Key; num = num % item.Key; if (repeatCount == 0) { continue; } for (int j = 0; j < repeatCount; j++) { result += item.Value; } } return result; } }