Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

roman to

public int RomanToInt(string s)
    {
        if (s == null || s == string.Empty)
            return 0;
        
        Dictionary<string, int> dict = new Dictionary<string, int>();
        int result = 0;
        
        dict.Add("I", 1);
        dict.Add("V", 5);
        dict.Add("X", 10);
        dict.Add("L", 50);
        dict.Add("C", 100);
        dict.Add("D", 500);
        dict.Add("M", 1000);
        dict.Add("IV", 4);
        dict.Add("IX", 9);
        dict.Add("XL", 40);
        dict.Add("XC", 90);
        dict.Add("CD", 400);
        dict.Add("CM", 900);
        
        for (int i = 0; i < s.Length; i++)
            if ((s[i] == 'I' || s[i] == 'X' || s[i] == 'C') && i < s.Length - 1 && dict.ContainsKey(s.Substring(i, 2)))
                result += dict[s.Substring(i++, 2)];
            else
                result += dict[s[i].ToString()];
        
        return result;   
    }
Comment

roman to

public int RomanToInt(string s)
    {
        if (s == null || s == string.Empty)
            return 0;
        
        Dictionary<string, int> dict = new Dictionary<string, int>();
        int result = 0;
        
        dict.Add("I", 1);
        dict.Add("V", 5);
        dict.Add("X", 10);
        dict.Add("L", 50);
        dict.Add("C", 100);
        dict.Add("D", 500);
        dict.Add("M", 1000);
        dict.Add("IV", 4);
        dict.Add("IX", 9);
        dict.Add("XL", 40);
        dict.Add("XC", 90);
        dict.Add("CD", 400);
        dict.Add("CM", 900);
        
        for (int i = 0; i < s.Length; i++)
            if ((s[i] == 'I' || s[i] == 'X' || s[i] == 'C') && i < s.Length - 1 && dict.ContainsKey(s.Substring(i, 2)))
                result += dict[s.Substring(i++, 2)];
            else
                result += dict[s[i].ToString()];
        
        return result;   
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# break from foreach method 
Csharp :: array join c# 
Csharp :: relative path c# 
Csharp :: check if file exist c# 
Csharp :: what is botnet attack 
Csharp :: max value data annotation c# 
Csharp :: .net core copy directory to output 
Csharp :: how to create a list c# 
Csharp :: get x and y of mouse uinty 
Csharp :: c# insert spaces before capital letters 
Csharp :: rigidbody velocity c# unity 
Csharp :: new ienumerable 
Csharp :: dotnet core 3.1 get the user that just logged in 
Csharp :: or c# 
Csharp :: array of strings by splitting lines c# 
Csharp :: c# string enum 
Csharp :: convert list string to list long c# 
Csharp :: .net core web app get dll name 
Csharp :: c# get battery level 
Csharp :: C# clear console input buffer 
Csharp :: where in used in linq c# 
Csharp :: verify if number c# 
Csharp :: c# Predicate delegate 
Csharp :: uri file path c# 
Csharp :: c# encrypted 
Csharp :: c# code to read txt file line by line and split 
Csharp :: Winform on exit run method 
Csharp :: get the number of cpu c# 
Csharp :: c# foreach namevaluecollection 
Csharp :: array declaration in c# 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =