forked from lolosssss/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_integer_to_roman.c
63 lines (51 loc) · 1.1 KB
/
12_integer_to_roman.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Description : Integer to Roman
* Given an integer, convert it to a roman numeral.
* Input is guaranteed to be within the range from 1 to 3999.
* Author : Evan Lau
* Date : 2016.02.25
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* intToRoman(int num)
{
int values[] = {
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
};
char symbols[13][13] = {
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
};
char *ret = (char *)malloc(sizeof(char) * 32);
int i = 0;
int j = 0;
int cur = 0;
int len = 0;
memset(ret, 0, sizeof(char) * 32);
while (num > 0)
{
int k = num / values[i];
for (j = 0; j < k; j++)
{
len = strlen(symbols[i]);
strncat(ret + cur, symbols[i], len);
cur += len;
num -= values[i];
}
i++;
}
return ret;
}
int main()
{
int n;
scanf("%d", &n);
printf("%s\n", intToRoman(n));
return 0;
}