diff --git a/solution/0000-0099/0012.Integer to Roman/README.md b/solution/0000-0099/0012.Integer to Roman/README.md index 815d37edba30b..c7151c82720b4 100644 --- a/solution/0000-0099/0012.Integer to Roman/README.md +++ b/solution/0000-0099/0012.Integer to Roman/README.md @@ -300,6 +300,30 @@ class Solution { } ``` +#### C + +```c +static const char* cs[] = { + "M", "CM", "D", "CD", "C", "XC", + "L", "XL", "X", "IX", "V", "IV", "I"}; + +static const int vs[] = { + 1000, 900, 500, 400, 100, 90, + 50, 40, 10, 9, 5, 4, 1}; + +char* intToRoman(int num) { + static char ans[20]; + ans[0] = '\0'; + for (int i = 0; i < 13; ++i) { + while (num >= vs[i]) { + num -= vs[i]; + strcat(ans, cs[i]); + } + } + return ans; +} +``` + diff --git a/solution/0000-0099/0012.Integer to Roman/README_EN.md b/solution/0000-0099/0012.Integer to Roman/README_EN.md index c7b57f780c463..9fcb12b9a56d2 100644 --- a/solution/0000-0099/0012.Integer to Roman/README_EN.md +++ b/solution/0000-0099/0012.Integer to Roman/README_EN.md @@ -298,6 +298,30 @@ class Solution { } ``` +#### C + +```c +static const char* cs[] = { + "M", "CM", "D", "CD", "C", "XC", + "L", "XL", "X", "IX", "V", "IV", "I"}; + +static const int vs[] = { + 1000, 900, 500, 400, 100, 90, + 50, 40, 10, 9, 5, 4, 1}; + +char* intToRoman(int num) { + static char ans[20]; + ans[0] = '\0'; + for (int i = 0; i < 13; ++i) { + while (num >= vs[i]) { + num -= vs[i]; + strcat(ans, cs[i]); + } + } + return ans; +} +``` + diff --git a/solution/0000-0099/0012.Integer to Roman/Solution.c b/solution/0000-0099/0012.Integer to Roman/Solution.c new file mode 100644 index 0000000000000..1417b40e44318 --- /dev/null +++ b/solution/0000-0099/0012.Integer to Roman/Solution.c @@ -0,0 +1,19 @@ +static const char* cs[] = { + "M", "CM", "D", "CD", "C", "XC", + "L", "XL", "X", "IX", "V", "IV", "I"}; + +static const int vs[] = { + 1000, 900, 500, 400, 100, 90, + 50, 40, 10, 9, 5, 4, 1}; + +char* intToRoman(int num) { + static char ans[20]; + ans[0] = '\0'; + for (int i = 0; i < 13; ++i) { + while (num >= vs[i]) { + num -= vs[i]; + strcat(ans, cs[i]); + } + } + return ans; +} diff --git a/solution/0000-0099/0013.Roman to Integer/README.md b/solution/0000-0099/0013.Roman to Integer/README.md index 4d985955579f8..604ebaaf5c8e8 100644 --- a/solution/0000-0099/0013.Roman to Integer/README.md +++ b/solution/0000-0099/0013.Roman to Integer/README.md @@ -341,6 +341,32 @@ def roman_to_int(s) end ``` +#### C + +```c +int nums(char c) { + switch (c) { + case 'I': return 1; + case 'V': return 5; + case 'X': return 10; + case 'L': return 50; + case 'C': return 100; + case 'D': return 500; + case 'M': return 1000; + default: return 0; + } +} + +int romanToInt(char* s) { + int ans = nums(s[strlen(s) - 1]); + for (int i = 0; i < (int) strlen(s) - 1; ++i) { + int sign = nums(s[i]) < nums(s[i + 1]) ? -1 : 1; + ans += sign * nums(s[i]); + } + return ans; +} +``` + diff --git a/solution/0000-0099/0013.Roman to Integer/README_EN.md b/solution/0000-0099/0013.Roman to Integer/README_EN.md index 099ed325658a3..5d93d580a88f1 100644 --- a/solution/0000-0099/0013.Roman to Integer/README_EN.md +++ b/solution/0000-0099/0013.Roman to Integer/README_EN.md @@ -327,6 +327,32 @@ def roman_to_int(s) end ``` +#### C + +```c +int nums(char c) { + switch (c) { + case 'I': return 1; + case 'V': return 5; + case 'X': return 10; + case 'L': return 50; + case 'C': return 100; + case 'D': return 500; + case 'M': return 1000; + default: return 0; + } +} + +int romanToInt(char* s) { + int ans = nums(s[strlen(s) - 1]); + for (int i = 0; i < (int) strlen(s) - 1; ++i) { + int sign = nums(s[i]) < nums(s[i + 1]) ? -1 : 1; + ans += sign * nums(s[i]); + } + return ans; +} +``` + diff --git a/solution/0000-0099/0013.Roman to Integer/Solution.c b/solution/0000-0099/0013.Roman to Integer/Solution.c new file mode 100644 index 0000000000000..7e3199c1e1fa7 --- /dev/null +++ b/solution/0000-0099/0013.Roman to Integer/Solution.c @@ -0,0 +1,21 @@ +int nums(char c) { + switch (c) { + case 'I': return 1; + case 'V': return 5; + case 'X': return 10; + case 'L': return 50; + case 'C': return 100; + case 'D': return 500; + case 'M': return 1000; + default: return 0; + } +} + +int romanToInt(char* s) { + int ans = nums(s[strlen(s) - 1]); + for (int i = 0; i < (int) strlen(s) - 1; ++i) { + int sign = nums(s[i]) < nums(s[i + 1]) ? -1 : 1; + ans += sign * nums(s[i]); + } + return ans; +}