12.整数转罗马数字

题目描述:

解法:

贪心法

class Solution {
public:
    string intToRoman(int num) {
        vector<int> number = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
        vector<string> roman={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        string result;
            for(int i =0;i<roman.size();i++){
                while(num>=number[i]){   //注意=,以及这里需要用while,不能用if
                    result.append(roman[i]);
                    num -= number[i];
                }
            }
        return result;
    }
};

原文地址:https://www.cnblogs.com/thefatcat/p/12246566.html

时间: 2024-08-01 01:01:39

12.整数转罗马数字的相关文章

[leetcode] 12. 整数转罗马数字

12. 整数转罗马数字 字符串处理,题超级简单,读懂题直接开干就行. 基本思路就是从大往小一点一点的来表示,具体看代码把: class Solution { public: string intToRoman(int num) { string ans = ""; int m = 0; m = num / 1000; for (int i = 0; i < m; i++) { ans += 'M'; } num %= 1000; if (num >= 900) { ans

LeetCode 12. 整数转罗马数字(Integer to Roman)

题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做  XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到

LeetCode 12 - 整数转罗马数字 - [简单模拟]

题目链接:https://leetcode-cn.com/problems/integer-to-roman/ 题解: 把 $1,4,5,9,10,40,50, \cdots, 900, 1000$ 均看做档位,优先转化大的档位,直到不能转化为止,然后降一个档位,继续转化,反复如此直到 $num=0$. AC代码: struct Solution { int o[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000}; string mp[13]; Solu

leetcode 12 整数转罗马数字 贪心

额,连着两个贪心? 这是局部最优问题:能用大“罗马数表示”就不会用小的. 先构造出所有基础罗马数,然后从大到小比较 因为比较的只有1000,900,...有限并有些麻烦,构造table  map<int,string> 然后,map默认安装按照key的值升序排序.. 想从大到小,用reverse_iterator class Solution { public: string intToRoman(int num) { map<int,string> calc = {{1000,&

LeetCode 12 Integer to Roman (整数转罗马数字)

题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", "M", "MM", "MMM”};//1000~3000String C[] = {"", "C", "CC", "CCC", "CD", "D&quo

Leetcode12---&gt;Integer to Roman(整数转换为罗马数字)

题目: 给定一个整数,将其转换为罗马数字; 题目很简单,主要是依靠整数和罗马数字的对应表: I= 1:V= 5: X = 10: L = 50: C = 100: D = 500: M = 1000 代码如下: 1 public class Solution { 2 public String intToRoman(int num) { 3 if(num <= 0) 4 return ""; 5 String[][] RomanDict = new String[][] { 6

LeetCode第12题 整数转罗马数字

/* 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做 XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边. 但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数

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整数转罗马数字

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