using System.Collections.Generic;
public static void Main()
Console.WriteLine($"III is 1 : {RomanToInt("I")}");
Console.WriteLine($"III is 3 : {RomanToInt("III")}");
Console.WriteLine($"XV is 15 : {RomanToInt("XV")}");
Console.WriteLine($"IV is 4 : {RomanToInt("IV")}");
Console.WriteLine($"IV is 8 : {RomanToInt("VIII")}");
Console.WriteLine($"XV is 14 : {RomanToInt("XIV")}");
Console.WriteLine($"MCMXCIV is 1994 : {RomanToInt("MCMXCIV")}");
public static int CalScale(char next, char c1, char c2)
return (next == c1 || next == c2) ? -1 : 1;
public static int RomanToInt(string s) {
int total = 0, size = s.Length;
for(int i = 0; i < size; i++)
char nextChar = (i + 1 < size ? s[i + 1] : '\0');
case 'I': total += 1 * CalScale(nextChar, 'V', 'X'); break;
case 'V': total += 5; break;
case 'X': total += 10 * CalScale(nextChar, 'C', 'L'); break;
case 'L': total += 50; break;
case 'C': total += 100 * CalScale(nextChar, 'M', 'D'); break;
case 'D': total += 500; break;
case 'M': total += 1000; break;
Console.WriteLine("No Roman");