Total Accepted: 43092
Total Submissions: 124603
Difficulty: Medium
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Show Similar Problems
Have you met this question in a real interview?
翻看了一些博客,发现以下这种是最简单的。
// test12IntegertoRoman.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "vector"
#include "string"
using std::vector;
using std::string;
string intToRoman(int num);
int _tmain(int argc, _TCHAR* argv[])
{
string s = intToRoman(1984);
return 0;
}
string intToRoman(int num)
{
vector<int> data = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
vector<string> romandata = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int length = data.size();
string result="";
for (int i = 0; i < length; i++)
{
while (num>=data[i])
{
num -= data[i];
result += romandata[i];
}
}
return result;
}