“Integer ke Roman” Kode Jawaban

Java int ke roman

import java.util.TreeMap;

public class RomanNumber {

    private final static TreeMap<Integer, String> map = new TreeMap<Integer, String>();

    static {

        map.put(1000, "M");
        map.put(900, "CM");
        map.put(500, "D");
        map.put(400, "CD");
        map.put(100, "C");
        map.put(90, "XC");
        map.put(50, "L");
        map.put(40, "XL");
        map.put(10, "X");
        map.put(9, "IX");
        map.put(5, "V");
        map.put(4, "IV");
        map.put(1, "I");

    }

    public final static String toRoman(int number) {
        int l =  map.floorKey(number);
        if ( number == l ) {
            return map.get(number);
        }
        return map.get(l) + toRoman(number-l);
    }

}
Bored Buffalo

Integer ke Roman

# Python 3 program to convert Decimal
# number to Roman numbers.
import math
 
def integerToRoman(A):
    romansDict = \
        {
            1: "I",
            5: "V",
            10: "X",
            50: "L",
            100: "C",
            500: "D",
            1000: "M",
            5000: "G",
            10000: "H"
        }
 
    div = 1
    while A >= div:
        div *= 10
 
    div //= 10
 
    res = ""
 
    while A:
 
        # main significant digit extracted
        # into lastNum
        lastNum = (A // div)
 
        if lastNum <= 3:
            res += (romansDict[div] * lastNum)
        elif lastNum == 4:
            res += (romansDict[div] +
                          romansDict[div * 5])
        elif 5 <= lastNum <= 8:
            res += (romansDict[div * 5] +
            (romansDict[div] * (lastNum - 5)))
        elif lastNum == 9:
            res += (romansDict[div] +
                         romansDict[div * 10])
 
        A = math.floor(A % div)
        div //= 10
         
    return res
 
# Driver code
print("Roman Numeral of Integer is:"
                   + str(integerToRoman(3549)))
Mansi Thakur

Jawaban yang mirip dengan “Integer ke Roman”

Pertanyaan yang mirip dengan “Integer ke Roman”

Lebih banyak jawaban terkait untuk “Integer ke Roman” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya