[LintCode] Integer to Roman 整数转化成罗马数字

Given an integer, convert it to a roman numeral.

The number is guaranteed to be within the range from 1 to 3999.

Have you met this question in a real interview?

Clarification

What is Roman Numeral?

Example

4 -> IV

12 -> XII

21 -> XXI

99 -> XCIX

more examples at: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm

LeetCode上的原题,请参见我之前的博客Integer to Roman

解法一:

class Solution {
public:
    /**
     * @param n The integer
     * @return Roman representation
     */
    string intToRoman(int n) {
        string res = "";
        vector<vector<string>> v {{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, {"M", "MM", "MMM"}};
        int cnt = 1000;
        for (int i = 3; i >= 0; --i) {
            int t = n / cnt;
            if (t) res += v[i][t - 1];
            n %= cnt;
            cnt /= 10;
        }
        return res;
    }
};

解法二:

class Solution {
public:
    /**
     * @param n The integer
     * @return Roman representation
     */
    string intToRoman(int n) {
        string res = "";
        vector<int> val {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        vector<string> str{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        for (int i = 0; i < val.size(); ++i) {
            while (n >= val[i]) {
                n -= val[i];
                res += str[i];
            }
        }
        return res;
    }
};

解法三:

class Solution {
public:
    /**
     * @param n The integer
     * @return Roman representation
     */
    string intToRoman(int n) {
        string res = "";
        vector<string> v1 {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
        vector<string> v2 {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
        vector<string> v3 {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
        vector<string> v4 {"", "M", "MM", "MMM"};
        return v4[n / 1000] + v3[(n % 1000) / 100] + v2[(n % 100) / 10] + v1[n % 10];
    }
};
时间: 2024-10-10 09:02:54

[LintCode] Integer to Roman 整数转化成罗马数字的相关文章

[LeetCode] Integer to Roman 整数转化成罗马数字

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 之前那篇文章写的是罗马数字转化成整数(http://www.cnblogs.com/grandyang/p/4120857.html), 这次变成了整数转化成罗马数字,基本算法还是一样.由于题目中限定了输入数字的范围(1 - 3999), 使得题目变得简单了不少. 基本字符 I V

第13题:整数转换成罗马数字&amp;第14题:罗马数字转换成整数

写在前面: 这两道题合起来写吧,其实整数转罗马数字我前天就写完了,当我想写罗马数转整数的时候竟然脑子一片空白,想了几分钟就想起来Map,本着学习的目的最终还是不想用Map,坚持C语言,今天脑子里直接涌出了Switch方式转换,看来"蹲在马桶上编程"的方式还是蛮不错的o(^▽^)o 整数转罗马数字:主要建立对应关系,输出时有点像百钱百鸡 罗马数字转整数:输入罗马数字(其实就是字符数组)后,for循环遍历,找出对应,这个再看不懂的建议去看看罗马数字表示方法. 第13题:整数转换成罗马数字

计蒜客 挑战难题 整数转换成罗马数字

给定一个整数num,( 1<=num<=3999),将整数转换成罗马数字. 如1,2,3,4,5对应的罗马数字分别位I,II,III,IV,V等. 格式: 第一行输入一个整数,接下来输出对应的罗马数字. 提示: 罗马数字的常识见此链接,对做题有帮助哦-尤其是表示方法. http://baike.baidu.com/link?url=injU8M4bAoc2zRZQ1GtgrfvuzCJO9PLnq6fpQGJLenakbzo-rS8p-qsYHR_81-aN 样例输入 123 样例输出 CXX

Python如何将整数转化成二进制字符串

Python 如何将整数转化成二进制字符串 1.你可以自己写函数采用 %2 的方式来算. >>> binary = lambda n: '' if n==0 else binary(n/2) + str(n%2) >>> binary(5) '101' >>> 2.采用 python 自带了方法 bin 函数,比如 bin(12345) 回返回字符串 '0b11000000111001', 这个时候在把0b去掉即可: >>> bin(

整数转换成罗马数字

编写一个将整数n(1 <= n <= 9999)转换成罗马数字. 整数n(1<=n<=9999)与罗马数字表示有以下对应关系 1000 - m,有几个1000就有几个m对应 900 - 两个字符cm 500 - 一个字符d 400 - 两个字符cd 100 - 一个字符c,有几个100就用几个c表示 90 - 两个字符xc 50 - 一个字符l 40 - 两个字符xl 10 - 一个字符x,有一个10就用几个x 9 - 用两个字符ix表示 5 - 用一个字符v来表示 4 - 用两个

[LeetCode]12. Integer to Roman整数转罗马数字

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which i

12. Integer to Roman 整数转罗马数字

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Solution:     def intToRoman(self, num):         """     

Leetcode 12 Integer to Roman整数变罗马

题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 翻译: 给一个整数,把他转换为罗马数字输出. 这道题是简单的字符转换的问题.只要了解罗马数字代表的含义即可. 罗马数字里面只有 1,5,10.  1到3之间用重复的1表示,4用IV 90用XC 依次类推. I = 1; V = 5; X = 10; L = 50; C = 1

[LintCode] Integer to Roman

Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range from 1 to 3999. Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals https://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%