Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
最开始的我还是太天真了。我甚至以为罗马符号只有X,V,I
我还是图simple啊……
“M”,”CM”,”D”,”CD”,”C”,”XC”,”L”,”XL”,”X”,”IX”,”V”,”IV”,”I”
1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
我的心好痛。
C# Code
public class Solution
{
public string IntToRoman(int num)
{
string[] ch = new string[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int[] value = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
string ans = "";
for (int i=0;num!=0;++i)
{
while (num >= value[i])
{
num -= value[i];
ans += ch[i];
}
}
return ans;
}
}
本文介绍了一种将整数转换为罗马数字的方法,并提供了一个使用C#实现的具体示例。输入范围限定在1到3999之间,通过定义罗马数字的符号及其对应的数值,采用减法原理进行转换。
2692

被折叠的 条评论
为什么被折叠?



