[LeetCode] 12.Integer to Roman
来源:互联网 发布:张玉宁维特斯数据 编辑:程序博客网 时间:2024/06/07 23:50
[LeetCode] 12.Integer to Roman
- 罗马数字与阿拉伯数字的关系
- 罗马数字的组数规则
- 题目描述
- 解题思路
- 实验代码
罗马数字与阿拉伯数字的关系
罗马数字的组数规则
1.基本数字 Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个;
2.不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个。
题目描述
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这些特殊的数字来达到题目要求的效果。
实验代码
class Solution {public: string intToRoman(int num) { string s; string Roman[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; int Integer[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; for (int i = 0; i < 13; i++) { while (num >= Integer[i]) { num -= Integer[i]; s += Roman[i]; } } return s; }};
阅读全文
0 0
- LeetCode 12. Integer to Roman
- [LeetCode]12.Integer to Roman
- LeetCode --- 12. Integer to Roman
- [Leetcode] 12. Integer to Roman
- [leetcode] 12.Integer to Roman
- leetcode 12. Integer to Roman
- leetcode 12. Integer to Roman
- [LeetCode]12. Integer to Roman
- leetcode 12. Integer to Roman
- LeetCode *** 12. Integer to Roman
- LeetCode 12. Integer to Roman
- leetcode 12. Integer to Roman
- leetcode 12. Integer to Roman
- Leetcode 12. Integer to Roman
- [Leetcode] 12. Integer to Roman
- leetcode 12. Integer to Roman
- 【leetcode】12. Integer to Roman
- Leetcode-12. Integer to Roman
- [LeetCode] 215. Kth Largest Element in an Array
- spring事务管理
- 实战5.Hive(下)--Hive实战
- 日常失了智的比赛01
- pat1003
- [LeetCode] 12.Integer to Roman
- 欢迎使用CSDN-markdown编辑器
- 最大长方形。
- 实战6.SparkSQL(上)--SparkSQL简介
- 面试-框架-springmvc
- pat1004
- pat1005
- pat1006
- Myeclipse运行程序时出现Console没有输出信息(只有<terminate>...)解决方法