【题目】
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
【分析】
I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;
还有一些特殊的:每两个阶段的之间有一个减法的表示,比如900=CM, C写在M前面表示M-C。
求商得到每个罗马文字的个数(如:3999 / 1000 = 3 结果有3个M)
【代码】
/*********************************
* 日期:2015-01-21
* 作者:SJF0115
* 题目: 12.Integer to Roman
* 网址:https://oj.leetcode.com/problems/integer-to-roman/
* 结果:AC
* 来源:LeetCode
* 博客:
**********************************/
#include <iostream>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
string result;
string roman[] = {"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};
int count;
// 转换为罗马数字
for(int i = 0;i < 13;++i){
count = num / value[i];
result += toRoman(count,roman[i]);
num = num % value[i];
}//if
return result;
}
private:
string toRoman(int num,string str){
string result;
for(int i = 0;i < num;++i){
result += str;
}//for
return result;
}
};
int main(){
Solution solution;
int num = 3999;
string result = solution.intToRoman(num);
// 输出
cout<<result<<endl;
return 0;
}
分享到:
相关推荐
LeetCode Roman to Integer解决方案
12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove ...
leetcode上Roman to Integer的完整C++代码,已被accepted
13. 罗马数字转整数 Roman to Integer用哈希存储映射字符--->对映的值对字符串的字符挨个判断,考虑下一个字符如果下一个字符大于当前字符,su
leetcode-integer_to_roman
12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter Combinations of a Phone Number 18.4Sum 19.Remove Nth Node From End
Leetcode回文串拼接 leetcode_node 题解 该项目主要用于基于...12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop 535 Encode and Decode TinyURL 560 565 566 Maximum Binary T
Up to date (2016-12-18), there are `447` Algorithms / `13` Database / `4` Shell / `4` Draft questions on [LeetCode Online Judge](https://leetcode.com/). The number of questions is increasing recently...
13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate ...
leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. ...
leetcode 跳跃 LeetCode Solved by Python easy/middle/hard:15/36/5 1. Two Sum 两数之和 2. Add Two ...Integer ...to Integer ...12. Integer to Roman 整数转罗马数字 13. Roman to Integer 罗马数字转
加油站 ...1.Roman to Integer 2.Binary Tree Level Order Travasal II 3.Binary Tree Level Order Travasal 4.Contains Duplicate II 5.Contains Duplicate 07/02/2015 1.Valid Parentheses 2.Maximum
leetcode 答案 LeetCode My LeetCode solution List 4. Longest Substring Without Repeating Characters: ...to ...12. Integer to Roman - >using this radix: mod = ['M','CM','D','CD','C','XC','L','XL'
12.integer-to-roman,自动创建文件夹和文件并使用vim打开文件(如下文件内容是自动生成的,根据剪贴板中的代码) 在 Solution 类中填写代码,保存退出 在终端输入leetcode commit ,即可将Solution类的代码复制到系统...
leetcode数组下标大于间距 5. Longest Palindromic Substring 这里使用menecher方法,就是动态规划,首先在原先的字符串之间插入'#, 这样可以统一处理奇数串和偶数串, 使用两个变量纪录状态, far_pos表示最长回文...
leetcode 答案力码 做LeetCode 1.【二和】(Array & Hash Table) (Easy) 20190911 完成 2.【加两个数】(链表&数学...12.【整数转罗马】(中)太难回答 13.【Roman To Integer】(中) 20181106完成的IIV是非法的? 14.
Roman to Integer 查表,通过从前往前筛选字符串,把代表的值一个个加起来 26. Remove Duplicates from Sorted Array 难度easy的题目。根据题目要求,是不能新建数组。只能在原来的基础上做修改。基本上这个算法类似...
(/problems/integer-to-english-words/) 22.1% 难278 25.4% 容易17 34.7% 中91 19.7% 中10 正则表达式匹配 (/problems/regular-expression-matching/) 24.1% 难253 38.9% 中15 21.6% 中277 寻找名人 (/problems/...
leetcode提交记录消失解决leetcode问题 ...https://leetcode.com/problems/roman-to-integer/description/ first-submission-successful : yes 2018-05-16 : - id : 172 type : math difficulty : easy url : ...
leetcode题库 LeetCode 题解合集 ...Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号生成 G