罗马数字转整数

 1,
        'V' => 5,
        'X' => 10,
        'L' => 50,
        'C' => 100,
        'D' => 500,
        'M' => 1000
    ];
    $sum = 0;
    foreach (str_split($s) as $key => $val) {
        if ($reps[$val] < 1 || $reps[$val] < $reps[$s[$key+1]]) $sum -= $reps[$val];
        else $sum += $reps[$val];
    }

    return $sum;
}

$s = 'DCXXI';#621
echo romanToInt($s);

你可能感兴趣的:(罗马数字转整数)