题目

将整数转换为罗马数字。罗马数字规则可以参考: 维基百科-罗马数字

解法

类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换。

代码

 class Solution {
public:
string intToRoman(int num) {
string result;
const int radix[] = {, , , , , , , , , , , , }; //基数,添加了40、90等计算之后的结果
const string sym[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; //各基数对应的罗马字符 for(int i = ; num > ; ++i)
{
int count = num / radix[i]; //当前基数的个数
num %= radix[i]; for( ; count > ; --count)
result += sym[i];
} return result;
}
};

LeetCode题解——Integer to Roman的更多相关文章

  1. [LeetCode 题解]: Interger to Roman

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an i ...

  2. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  3. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  4. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  5. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  6. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  7. LeetCode——12. Integer to Roman

    一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 【Linux高频命令专题(20)】du

    概述 显示每个文件和目录的磁盘使用空间. 命令格式 du [选项][文件] 文件缺省就代表当前目录大小 参数 -a或-all 显示目录中个别文件的大小. -b或-bytes 显示目录或文件大小时,以b ...

  2. CentOS 7.0安装Nvidia驱动

    entOS 7.0 Nvidia显卡安装步骤: 1 在英伟达官网下载相应驱动 搜索出相应的驱动后,不要直接点,而是右健,Save Link as... 否则,会出现下载半天没动静的情况. 存放的路径上 ...

  3. Haxe数据类型

    以下是Haxe里面的一些数据类型 基本类型 空特性 类 枚举 匿名结构 方法 动态 抽象 1. 基本类型Bool, Float, Int 2. 空特性由于Haxe可以被编译为各种不同的target,不 ...

  4. spring-boot-quartz, 依赖spring-boot-parent

    spring-boot-quartz, 依赖spring-boot-parent spring-boot Easyui Quartz 项目启动后输入:http://localhost/ 数据库文件:  ...

  5. sublime安装插件

    今天因为某些原因,把 sublime 卸载掉了,然后来安装的时候,发现 Package Control  无法安装了,或者安装好后运行 ctrl + shift + p,会报 错误,截图如下: 然后就 ...

  6. Android之界面刷新(invalidate和postInvalidate使用)

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...

  7. Struts2系列——struts2的result

    在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作. 在ActionSupport基类中定 ...

  8. 最大流算法(Edmons-Karp + Dinic 比较) + Ford-Fulkson 简要证明

    Ford-Fulkson用EK实现:483ms #include <cstdio> #include <cstring> #define min(x,y) (x>y?y: ...

  9. 利用dns解析来实现网站的负载均衡

    当网站的访问量大了就会考虑负载均衡,这也是每一个架构师的基本功了,其基本地位就相当于相声里的说学逗唱,活好不好就看这个了 :) 传统的负载均衡思路是单点的,不管你是硬件的还是软件的基本都是这样的原理 ...

  10. WebServices生成发布过程及常见问题的解决方法

    春夏秋冬走健康之路看四季养生网 健康饮食 养生问题 母婴保健 养生小常识 3.下一步,我们需要将Myservice文件夹拷贝到C:\Inetpub\wwwroot目录下(重要).如下图所示  然后依次 ...