12. Integer to Roman
2016-04-03 22:21
423 查看
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
题意:整数转为罗马序列。
思路:把1000,900,500,400,100,90,50,40,10,9,5,4,1作为基数,然后进行除法和取余运算。
Input is guaranteed to be within the range from 1 to 3999.
题意:整数转为罗马序列。
思路:把1000,900,500,400,100,90,50,40,10,9,5,4,1作为基数,然后进行除法和取余运算。
class Solution { public: string intToRoman(int num) { string s; int base[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; string roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; int i = 0; while (num){ int j = 0; j = num / base[i]; num %= base[i]; while (j--){ s += roman[i]; } i++; } return s; } };
相关文章推荐
- 结对项目:计算器 第二篇
- 《构建之法》第四章读后感
- 集锦
- 深入理解HTTP协议、HTTP协议原理分析
- 软件工程第四个程序:结对项目 —— 电梯调度问题(实现)
- 第五周项目1-三角形类的雏形(5)
- CERC 2009 Cav 扫描法 (uvaLive 4621 - Cav)
- socket server 服务端 带计算心跳。超时断链的控制
- 修正后的时间类
- Hive与HBase的区别
- block,inline和inline-block概念和区别
- 封装
- 确定两个字符串通过重新排序是否可以相同的算法
- Linux内核设计第六周 ——进程的描述和创建
- cf19B. Checkout Assistant 【01 背包】
- 安卓打开File Explorer里面不显示内容
- socket server 长连接服务端
- 安卓打开File Explorer里面不显示内容
- lintcode:Unique Binary Search Trees II
- Android设计模式应用--状态模式