xinglie.github.io icon indicating copy to clipboard operation
xinglie.github.io copied to clipboard

罗马转数字

Open xinglie opened this issue 1 year ago • 0 comments

let romanToNumbers = {
    I: 1,
    V: 5,
    X: 10,
    L: 50,
    C: 100,
    D: 500,
    M: 1000
};

let romanToInt = roman => {
    let n = 0;
    let total = roman.length;
    let index = 0;
    while (index < total) {
        let current = roman[index],
            next = roman[index + 1];
        let currentValue = romanToNumbers[current];
        let nextValue = romanToNumbers[next];
        if (currentValue < nextValue) {
            n += nextValue - currentValue;
            index++;
        } else {
            n += currentValue;
        }
        index++;
    }
    return n;
};

xinglie avatar Mar 07 '25 09:03 xinglie