12. Integer to Roman
来源:互联网 发布:万能搬家软件 编辑:程序博客网 时间:2024/06/15 22:06
Description:
iven an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路:题目还是算简单的吧,规定了1到3999的范围,我直接利用了贪心算法,每次都取最大的那个数,组合起来就解决了问题
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; } };
0 0
- 12.Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 单调递增最长子序列nyoj17
- struts2_08_global-results(全局结果集)
- “stdafx.h”: No such file or directory
- Windows的关机和重起
- servlet
- 12. Integer to Roman
- Unity 5.6推出 改进图形性能以及支持Vulkan API
- curl -- Qt中使用FTP传递文件
- ExtJS隐藏和显示的操作
- ubuntu pip 报错
- 软件测试笔试题(1)
- android Facebook登陆获取用户信息
- 【毒鸡汤】基层管理如果没有这些心态,难!
- 文献类型标识