企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一.题目描述 Given a roman numeral, convert it to an integer.  Input is guaranteed to be within the range from 1 to 3999. ## 二.题目分析 罗马数字总结: 1~9: {“I”, “II”, “III”, “IV”, “V”, “VI”, “VII”, “VIII”, “IX”}; 10~90: {“X”, “XX”, “XXX”, “XL”, “L”, “LX”, “LXX”, “LXXX”, “XC”}; 100~900: {“C”, “CC”, “CCC”, “CD”, “D”, “DC”, “DCC”, “DCCC”, “CM”}; 1000~3000: {“M”, “MM”, “MMM”}. 关于罗马数制可参考:[http://baike.baidu.com/view/1246899.htm](http://baike.baidu.com/view/1246899.htm) 这个问题就是输入个阿拉伯数字,将其转换为罗马数字字符串。 该题可使用最简单的操作,即取数字的低位到高位进行操作。每次取一位,取完一位则数字除10,取出的那一位数字则用罗马数字表示出来。需要注意的是如果当前位不是原数字的各位则需要扩大10倍或者100倍等。 ## 三.示例代码 ~~~ class Solution { public: string intToRoman(int num) { string str; string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; int value[]= {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; for(int i=0;num!=0;++i) { while(num>=value[i]) { num-=value[i]; str+=symbol[i]; } } return str; } }; ~~~