add prob 7

This commit is contained in:
2025-08-22 00:10:06 +08:00
parent 7949680bb4
commit eb2426e4de

63
7-13.c Normal file
View File

@@ -0,0 +1,63 @@
#include <stdio.h>
#include <string.h>
int romanToInt(char* s) {
int ans = 0;
int len = strlen(s);
for (int i = 0; i < len; ++i) {
char cur = s[i];
char peeknext;
if (i == len - 1) {
peeknext = ' ';
} else {
peeknext = s[i + 1];
}
switch (cur)
{
case 'I':
if (peeknext == 'V' || peeknext == 'X') {
ans -= 1;
} else {
ans += 1;
}
break;
case 'V':
ans += 5;
break;
case 'X':
if (peeknext == 'L' || peeknext == 'C') {
ans -= 10;
} else {
ans += 10;
}
break;
case 'L':
ans += 50;
break;
case 'C':
if (peeknext == 'D' || peeknext == 'M') {
ans -= 100;
} else {
ans += 100;
}
break;
case 'D':
ans += 500;
break;
case 'M':
ans += 1000;
break;
default:
break;
}
}
return ans;
}
int main() {
char s[] = "LVIII";
printf("%d\n", romanToInt(s));
return 0;
}