
数字和罗马数字互转localROMAN{{1000,M},{900,CM},{500,D},{400,CD},{100,C},{90,XC},{50,L},{40,XL},{10,X},{9,IX},{5,V},{4,IV},{1,I},}localVALUES{I1,V5,X10,L50,C100,D500,M1000,}functionconvertRoman(value)iftype(value)numberthenassert(value1andvalue3999andvalue%10,只支持 1 到 3999 的整数)localresult{}for_,iteminipairs(ROMAN)dolocalcountmath.floor(value/item[1])ifcount0thenresult[#result1]string.rep(item[2],count)valuevalue%item[1]endendreturntable.concat(result)endlocaltextstring.upper(tostring(value))localtotal,i0,1whilei#textdolocalcurrentVALUES[text:sub(i,i)]localnextValueVALUES[text:sub(i1,i1)]assert(current,非法罗马数字)ifnextValueandcurrentnextValuethentotaltotalnextValue-current ii2elsetotaltotalcurrent ii1endendreturntotalend运行convertRoman(1994)-- MCMXCIVconvertRoman(XIV)-- 14