-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathromanToInt.cs
More file actions
40 lines (35 loc) · 1.01 KB
/
romanToInt.cs
File metadata and controls
40 lines (35 loc) · 1.01 KB
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
using System;
namespace treinoObjetos
{
public class Solution
{
public int RomanToInt(string s)
{
int num = 0;
Dictionary<char, int> numeros = new Dictionary<char, int>
{
{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50},
{'C', 100}, {'D', 500}, {'M', 1000}
};
for (int i = 0; i < s.Length; i++)
{
if (i < s.Length - 1 && numeros[s[i]] < numeros[s[i + 1]])
{
num -= numeros[s[i]];
}
else
{
num += numeros[s[i]];
}
}
return num;
}
static void Main(string[] args)
{
Solution solution = new Solution();
Console.Write("\nDigite um número em algarismos romanos: ");
string num = Console.ReadLine().ToUpper();
Console.WriteLine("\n" + solution.RomanToInt(num));
}
}
}