Given an integer, convert it to a Roman numeral.
Input is guaranteed to being within the range from 1 to 3999.
Solution:
Take 3999, for example:
Class Solution {
Public
string inttoroman (int num) {
if (num<1| | num>3999) return NULL;
String str;
while (num) {
if (num/1000!=0) {
for (int i=0;i<num/1000;i++) str+= "M";
num=num%1000;
}else if (num/100!=0) {
int tmp=num/100;
if (tmp%5==4) {
if (tmp/5==0) {
str+= "C"; str+= "D";
}else{
str+= "C"; str+= "M";
}
}else{
if (tmp/5!=0) {
str+= "D";
}
for (int i=0;i<tmp%5;i++) str+= "C";
}
num=num%100;
}else if (num/10!=0) {
int TMP=NUM/10;
if (tmp%5==4) {
if (tmp/5==0) {
str+= "X"; str+= "L";
}else{
str+= "X"; str+= "C";
}
}else{
if (tmp/5!=0) {
str+= "L";
}
for (int i=0;i<tmp%5;i++) str+= "X";
}
num=num%10;
}else{
if (num%5==4) {
if (num/5==0) {
str+= "I"; str+= "V";
}else{
str+= "I"; str+= "X";
}
}else{
if (num/5!=0) {
str+= "V";
}
for (int i=0;i<num%5;i++) str+= "I";
}
num=num%1;
}
}
return str;
}
};
Integer to Roman (c + +)