12. Integer to Roman
来源:互联网 发布:大家的日语 知乎 编辑:程序博客网 时间:2024/05/05 10:16
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路:任何一种计数法都是基于一些base,每个base代表一个scale的数字,然后累加就可以了。
http://bangbingsyb.blogspot.jp/2014/11/leetcode-integer-to-roman.html
class Solution {public: string intToRoman(int num) { string dict[13] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; int val[13] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; string ans; int count; for(int i = 0; i < 13; i++) { if(num >= val[i]) { count = num/val[i]; num = num % val[i]; for(int j = 0; j < count; j++) { ans.append(dict[i]); } } } return ans; }};
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
- windows下安装nginx
- HDU-3592 World Exhibition
- js数据类型回顾
- hdu 5791 Two
- Android中Dialog、Toast和Snackbar使用场景以及技巧
- 12. Integer to Roman
- 异步任务之软件下载【Android】
- 十分钟搞定pandas
- 贪心 POJ 3040
- nginx.conf文件说明
- web前端常用长度单位(px,em,rem,pt)
- Oracle RAC 集群启动与停止
- poj 3274 Gold Balanced Lineup
- 【杭电】[2196]Computer