题目链接:https://leetcode-cn.com/problems/integer-to-roman/
题解:
把 $1,4,5,9,10,40,50, cdots, 900, 1000$ 均看做档位,优先转化大的档位,直到不能转化为止,然后降一个档位,继续转化,反复如此直到 $num=0$。
AC代码:
struct Solution { int o[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000}; string mp[13]; Solution() { mp[0]="I", mp[1]="IV", mp[2]="V", mp[3]="IX"; mp[4]="X", mp[5]="XL", mp[6]="L", mp[7]="XC"; mp[8]="C", mp[9]="CD", mp[10]="D", mp[11]="CM", mp[12]="M"; } string intToRoman(int num) { string res; int p=12; while(num>0) { while(num<o[p]) p--; while(num>=o[p]) { num-=o[p]; res+=mp[p]; } } return res; } };