这道题就是先求出各个位上1~9的表示方法 然后进行拼接
public class Solution {
public String intToRoman(int num) {
String result = "";
int shang = num / 1000;
num = num % 1000;
if (shang > 0) {
for (int i = 0; i < shang; i++) {
result = result + "M";
}
}
shang = num / 100;
num = num % 100;
if (shang > 0) {
if (shang == 9) {
result = result + "CM";
} else if (shang >= 5) {
result = result + "D";
shang = shang - 5;
for (int i = 0; i < shang; i++) {
result = result + "C";
}
} else if (shang == 4) {
result = result + "CD";
} else {
for (int i = 0; i < shang; i++) {
result = result + "C";
}
}
}
shang = num / 10;
num = num % 10;
if (shang > 0) {
if (shang == 9) {
result = result + "XC";
} else if (shang >= 5) {
result = result + "L";
shang = shang - 5;
for (int i = 0; i < shang; i++) {
result = result + "X";
}
} else if (shang == 4) {
result = result + "XL";
} else {
for (int i = 0; i < shang; i++) {
result = result + "X";
}
}
}
shang = num;
if (shang > 0) {
if (shang == 9) {
result = result + "IX";
} else if (shang >= 5) {
result = result + "V";
shang = shang - 5;
for (int i = 0; i < shang; i++) {
result = result + "I";
}
} else if (shang == 4) {
result = result + "IV";
} else {
for (int i = 0; i < shang; i++) {
result = result + "I";
}
}
}
return result;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。