博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
integer to roman leetcode c++实现
阅读量:6246 次
发布时间:2019-06-22

本文共 1314 字,大约阅读时间需要 4 分钟。

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

将整数用罗马数字表示。

思路分析:  

{"","I","II","III","IV","V","VI","VII","VIII","IX"},

{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
{"","M","MM","MMM"}

    首先从输入的整数开始,(1)取其个位上的数值,判定后得到一个字符串(这个字符串也应该位于罗马数字的最右边,所以再用一个栈来解决)

               (2)然后将整数除以10(轮到十位上的数值了),再去执行(1)

代码如下:

    

1 #include
2 class Solution { 3 public: 4 string intToRoman(int num) { 5 vector
> data={ 6 {
"","I","II","III","IV","V","VI","VII","VIII","IX"}, 7 {
"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}, 8 {
"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}, 9 {
"","M","MM","MMM"}10 };11 stack
sta;12 int i=0;13 string res="",result="";14 while(num!=0){15 int remain=num%10;16 res=data[i][remain];17 sta.push(res);18 ++i;19 num/=10;20 }21 while(!sta.empty()){22 result+=sta.top();23 sta.pop();24 }25 return result;26 }27 };

转载于:https://www.cnblogs.com/chess/p/4682143.html

你可能感兴趣的文章
当寂寞不得不成为一种习惯
查看>>
oracle的序列号(sequence)
查看>>
MyEclipse启动tomcat发生Socket bind failed: [730048]
查看>>
树莓派连接到手机屏幕
查看>>
MyBatis学习整理0
查看>>
[转载]不再让你孤单
查看>>
登录验证的生成类RandomCodeRender
查看>>
singleton
查看>>
smarty插件判断图片是否存在,不存在则调用默认图片
查看>>
[转载] 晓说——第29期:海上霸主航母(上)
查看>>
05 显示网页信息
查看>>
[转载] 中华典故故事(孙刚)——37 只许州官放火,不许百姓点灯
查看>>
mysql5.7.22源码编译安装
查看>>
Java基础学习总结(23)——GUI编程
查看>>
SVN学习总结(2)——SVN冲突解决
查看>>
nagios的安装搭建以及添加监控主机
查看>>
Harbor和YUM部署for CentOS 7
查看>>
shell脚本练习一(if语句、case语句、for语句、while语句)
查看>>
Web服务(二)httpd配置参数详细介绍
查看>>
unity中射线碰撞检测总结
查看>>