描述
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
#include <iostream>
#include<string>
#include<vector>
using namespace std;
const int n = 13;
const int index[n] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
const string roman[n] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string IntergerToRoman(int num)
{
string Result;
if (num <= 0 || num >= 4000)
return Result;
for (int i = 0; num>0; i++)
{
int temp = num / index[i];
num = num%index[i];
for (int j = temp; j>0; j--)
{
Result += roman[i];
}
}
return Result;
}
int main()
{
int num = 2225;
string Result = IntergerToRoman(num);
cout << Result << endl;
}