forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.c
85 lines (83 loc) · 1.83 KB
/
12.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
char *intToRoman(int num) {
char *s = (char *)calloc(10, sizeof(char));
int i = 0;
while (num > 0) {
while (num >= 1000) {
s[i] = 'M';
num -= 1000;
i++;
}
if (num >= 900) {
s[i] = 'C'; s[i + 1] = 'M';
i += 2;
num -= 900;
}
while (num >= 500) {
s[i] = 'D';
num -= 500;
i++;
}
if (num >= 400) {
s[i] = 'C'; s[i + 1] = 'D';
i += 2;
num -= 400;
}
while (num >= 100) {
s[i] = 'C';
num -= 100;
i++;
}
if (num >= 90) {
s[i] = 'X'; s[i + 1] = 'C';
i += 2;
num -= 90;
}
while (num >= 50) {
s[i] = 'L';
num -= 50;
i++;
}
if (num >= 40) {
s[i] = 'X'; s[i + 1] = 'L';
i += 2;
num -= 40;
}
while (num >= 10) {
s[i] = 'X';
num -= 10;
i++;
}
if (num >= 9) {
s[i] = 'I'; s[i + 1] = 'X';
i += 2;
num -= 9;
}
while (num >= 5) {
s[i] = 'V';
num -= 5;
i++;
}
if (num >= 4) {
s[i] = 'I'; s[i + 1] = 'V';
i += 2;
num -= 4;
}
while (num >= 1) {
s[i] = 'I';
num -= 1;
i++;
}
}
return s;
}
int main() {
int a = 2014;
int b = 2015;
/* should be MMXIV */
printf("%s\n", intToRoman(a));
/* should be MMXV */
printf("%s\n", intToRoman(b));
return 0;
}