/*
* Leetcode12. Integer to Roman
* Funtion: Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.
* Author: LKJ
* Date:2016/7/27
* Hint: Related to12. Roman to Integer
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
char* Roman[4][10] = {
{"","I","II","III","IV","V","VI","VII","VIII","IX"},
{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
{"","M","MM","MMM"}
};
string result;
result.append(Roman[3][num/1000]);
result.append(Roman[2][num%1000/100]);
result.append(Roman[1][num%100/10]);
result.append(Roman[0][num%10]);
return result;
}
};
int main(){
int myin;
string myout;
Solution SA;
cout << "Please Enter" << endl;
cin >> myin;
myout = SA.intToRoman(myin);
cout << myout << endl;
return 0;
}
LeetCode 简单操作 | 12. Integer to Roman
最新推荐文章于 2023-07-01 19:06:08 发布
本文介绍了一种将整数转换为罗马数字的算法实现,适用于1到3999范围内的整数转换。该算法使用了数组来映射不同位上的数值与对应的罗马数字符号,通过简单的数学运算完成转换。
330

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



