Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 28cace0

Browse files
feat: add c solutions to lc problems: No.0012,0013 (#4511)
1 parent 6d918ed commit 28cace0

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

‎solution/0000-0099/0012.Integer to Roman/README.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,30 @@ class Solution {
300300
}
301301
```
302302

303+
#### C
304+
305+
```c
306+
static const char* cs[] = {
307+
"M", "CM", "D", "CD", "C", "XC",
308+
"L", "XL", "X", "IX", "V", "IV", "I"};
309+
310+
static const int vs[] = {
311+
1000, 900, 500, 400, 100, 90,
312+
50, 40, 10, 9, 5, 4, 1};
313+
314+
char* intToRoman(int num) {
315+
static char ans[20];
316+
ans[0] = '0円';
317+
for (int i = 0; i < 13; ++i) {
318+
while (num >= vs[i]) {
319+
num -= vs[i];
320+
strcat(ans, cs[i]);
321+
}
322+
}
323+
return ans;
324+
}
325+
```
326+
303327
<!-- tabs:end -->
304328

305329
<!-- solution:end -->

‎solution/0000-0099/0012.Integer to Roman/README_EN.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,30 @@ class Solution {
298298
}
299299
```
300300

301+
#### C
302+
303+
```c
304+
static const char* cs[] = {
305+
"M", "CM", "D", "CD", "C", "XC",
306+
"L", "XL", "X", "IX", "V", "IV", "I"};
307+
308+
static const int vs[] = {
309+
1000, 900, 500, 400, 100, 90,
310+
50, 40, 10, 9, 5, 4, 1};
311+
312+
char* intToRoman(int num) {
313+
static char ans[20];
314+
ans[0] = '0円';
315+
for (int i = 0; i < 13; ++i) {
316+
while (num >= vs[i]) {
317+
num -= vs[i];
318+
strcat(ans, cs[i]);
319+
}
320+
}
321+
return ans;
322+
}
323+
```
324+
301325
<!-- tabs:end -->
302326

303327
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
static const char* cs[] = {
2+
"M", "CM", "D", "CD", "C", "XC",
3+
"L", "XL", "X", "IX", "V", "IV", "I"};
4+
5+
static const int vs[] = {
6+
1000, 900, 500, 400, 100, 90,
7+
50, 40, 10, 9, 5, 4, 1};
8+
9+
char* intToRoman(int num) {
10+
static char ans[20];
11+
ans[0] = '0円';
12+
for (int i = 0; i < 13; ++i) {
13+
while (num >= vs[i]) {
14+
num -= vs[i];
15+
strcat(ans, cs[i]);
16+
}
17+
}
18+
return ans;
19+
}

‎solution/0000-0099/0013.Roman to Integer/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,32 @@ def roman_to_int(s)
341341
end
342342
```
343343

344+
#### C
345+
346+
```c
347+
int nums(char c) {
348+
switch (c) {
349+
case 'I': return 1;
350+
case 'V': return 5;
351+
case 'X': return 10;
352+
case 'L': return 50;
353+
case 'C': return 100;
354+
case 'D': return 500;
355+
case 'M': return 1000;
356+
default: return 0;
357+
}
358+
}
359+
360+
int romanToInt(char* s) {
361+
int ans = nums(s[strlen(s) - 1]);
362+
for (int i = 0; i < (int) strlen(s) - 1; ++i) {
363+
int sign = nums(s[i]) < nums(s[i + 1]) ? -1 : 1;
364+
ans += sign * nums(s[i]);
365+
}
366+
return ans;
367+
}
368+
```
369+
344370
<!-- tabs:end -->
345371
346372
<!-- solution:end -->

‎solution/0000-0099/0013.Roman to Integer/README_EN.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,32 @@ def roman_to_int(s)
327327
end
328328
```
329329

330+
#### C
331+
332+
```c
333+
int nums(char c) {
334+
switch (c) {
335+
case 'I': return 1;
336+
case 'V': return 5;
337+
case 'X': return 10;
338+
case 'L': return 50;
339+
case 'C': return 100;
340+
case 'D': return 500;
341+
case 'M': return 1000;
342+
default: return 0;
343+
}
344+
}
345+
346+
int romanToInt(char* s) {
347+
int ans = nums(s[strlen(s) - 1]);
348+
for (int i = 0; i < (int) strlen(s) - 1; ++i) {
349+
int sign = nums(s[i]) < nums(s[i + 1]) ? -1 : 1;
350+
ans += sign * nums(s[i]);
351+
}
352+
return ans;
353+
}
354+
```
355+
330356
<!-- tabs:end -->
331357
332358
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int nums(char c) {
2+
switch (c) {
3+
case 'I': return 1;
4+
case 'V': return 5;
5+
case 'X': return 10;
6+
case 'L': return 50;
7+
case 'C': return 100;
8+
case 'D': return 500;
9+
case 'M': return 1000;
10+
default: return 0;
11+
}
12+
}
13+
14+
int romanToInt(char* s) {
15+
int ans = nums(s[strlen(s) - 1]);
16+
for (int i = 0; i < (int) strlen(s) - 1; ++i) {
17+
int sign = nums(s[i]) < nums(s[i + 1]) ? -1 : 1;
18+
ans += sign * nums(s[i]);
19+
}
20+
return ans;
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /