“Romawi untuk bilangan bulat” Kode Jawaban

Python Roman ke Integer

class Solution:
    def romanToInt(self, s):
 
        values = {'I': 1, 'V': 5, 'X': 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}   
        result = 0
        for i in range(len(s)):
            if i + 1 < len(s) and values[s[i]] < values[s[i + 1]] : # if current item is not the last item on the string
                                                                    # and current item's value is smaller than next item's value 
                result = result - values[s[i]]                      # then subtract current item's value from result
            else:
                result = result + values[s[i]]                      # otherwise add current item's value to result
        return result

Task = Solution()
print(Task.romanToInt("III"))
print(Task.romanToInt("LVIII"))
print(Task.romanToInt("MCMXCIV"))
Kingsley Atuba

Romawi untuk bilangan bulat

class Solution:
    def romanToInt(self, s):
 
        values = {'I': 1, 'V': 5, 'X': 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}   
        result = 0
        for i in range(len(s)):
            if i + 1 == len(s) or values[s[i]] >= values[s[i + 1]] : # if current item is not the last item on the string
                                                                    # or current item's value is greater than or equal to next item's value 
                result = result + values[s[i]]                      # then add current item's value from result
            else:
                result = result - values[s[i]]                      # otherwise subtract current item's value from result
        return result

Task = Solution()
print(Task.romanToInt("III"))
print(Task.romanToInt("LVIII"))
print(Task.romanToInt("MCMXCIV"))
Kingsley Atuba

Romawi untuk bilangan bulat

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000
Light Lizard

Romawi untuk bilangan bulat

string[] s = { "I", "V", "X", "L", "C", "D", "M" };
            int[] r = { 1, 5, 10, 50, 100, 500, 1000 };
            string roman;
            roman = Console.ReadLine();
            int l = roman.Length;
            int n = 0;
            for (int i = 0; i < roman.Length; i++)
            {
                for (int j = 0; j < s.Length; j++)
                {
                    if (roman[i].ToString() == s[j])
                    {
                        for (int k = 0; k < s.Length; k++)
                        {
                            if (i + 1 < roman.Length)
                            {
                                if (roman[i + 1].ToString() == s[k])
                                {
                                    if (k > j)
                                    {
                                        n -= r[j];
                                    }
                                    else
                                    {
                                        n += r[j];
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                n += r[j];
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            Console.WriteLine(n);
Info Important

Romawi untuk bilangan bulat

var romanToInt = function (s) {

  const object = {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000
  }
  let count =0
  console.log(object)
 
 for(i =0; i<s.length;i++){
   const cur = object[s[i]]
   const next = object[s[i+1]]

 if(cur<next){
   count += next-cur
   i++
 }else{
   count+= cur
 }
}
return count

};
ABS

Jawaban yang mirip dengan “Romawi untuk bilangan bulat”

Pertanyaan yang mirip dengan “Romawi untuk bilangan bulat”

Lebih banyak jawaban terkait untuk “Romawi untuk bilangan bulat” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya