forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteger_to_Roman.cpp
46 lines (40 loc) · 1.06 KB
/
Integer_to_Roman.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Author: Weixian Zhou, [email protected]
Date: Jun 29, 2012
Problem: Integer to Roman
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Solution:
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
string roman;
string digits[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
string tens[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string hundreds[10] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
int thousands = num / 1000;
for (int i = 0; i < thousands; i++) {
roman += 'M';
}
num = num - thousands * 1000;
roman += hundreds[num / 100];
num = num - num / 100 * 100;
roman += tens[num / 10];
num = num - num / 10 * 10;
roman += digits[num];
return roman;
}
};