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)。

第一步,按照千分位,百分位...一一算下来。

class Solution {
public:
string intToRoman(int num) {
int quote;
int residue;
string ret = ""; //first deal with 10^3
quote = num/;
residue = num%;
while(quote > ){
ret += 'M';
quote--;
} //then deal with 10^2
quote = residue/;
residue %= ;
if(quote==){
ret += "CM";
}
else if(quote >=){
ret += 'D';
while(quote > ){
ret += 'C';
quote--;
}
}
else if(quote==){
ret += "CD";
}
else{
while(quote > ){
ret += 'C';
quote--;
}
} //then deal with 10
quote = residue/;
residue %= ;
if(quote==){
ret += "XC";
}
else if(quote >=){
ret += 'L';
while(quote > ){
ret += 'X';
quote--;
}
}
else if(quote==){
ret += "XL";
}
else{
while(quote > ){
ret += 'X';
quote--;
}
} //finally deal with 1
quote = residue;
if(quote==){
ret += "IX";
}
else if(quote >=){
ret += 'V';
while(quote > ){
ret += 'I';
quote--;
}
}
else if(quote==){
ret += "IV";
}
else{
while(quote > ){
ret += 'I';
quote--;
}
} return ret;
}
};

第二步,代码有冗余,将其归纳整合。并且用减法代替除法!

class Solution {
public:
string intToRoman(int num) {
int values[] = {, , , , , , , , , , , , }; //数组的初始化
string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int size = sizeof(values) / sizeof(values[]); //获取数组的大小
string result = ""; for (int i = ; i < size; i++) {
while (num >= values[i]) {
num -= values[i];
result+=numerals[i];
}
}
return result;
}
};

12. Integer to Roman (HashTable)的更多相关文章

  1. Leetcode 12——Integer to Roman

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

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

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

  3. leetCode练题——12. Integer to Roman

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

  4. 《LeetBook》leetcode题解(12):Integer to Roman[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. 【LeetCode】12. Integer to Roman (2 solutions)

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

  6. [LeetCode] 12. Integer to Roman ☆☆

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

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

  8. 12. Integer to Roman

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. 【LeetCode】12. Integer to Roman 整型数转罗马数

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

随机推荐

  1. Android 4.3发布 新增4大改变25日推送升级[附Android 4.3 工厂镜像]

    北京时间7月25日,谷歌举行发布会,正式发布了全新的Nexus 7平板电脑以及Android 4.3系统. 其中Android 4.3系统隶属于4.X果冻豆(Jelly Bean)系列,是目前最新的操 ...

  2. 5.3 将users表添加到xadmin后台

    在users模块中添加adminx.py文件,是xadmin后台管理默认的文件名,内容是: from .models import EmailVerifyRecord, Banner import x ...

  3. 判断一颗二叉树是否为二叉平衡树 python 代码

    输入一颗二叉树,判断这棵树是否为二叉平衡树.首先来看一下二叉平衡树的概念:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.因此判断一颗二叉平衡树的关键在于 ...

  4. IOS layoutSubviews总结

    ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit - (void)layoutSubviews - (voi ...

  5. Linux运维学习笔记-文件系统知识体系总结

    文件系统知识总结 新买的硬盘要存放数据需要怎么做? 首先将硬盘装机做RAID,做完RAID后进行分区,分完区后格式化创建文件系统,最后存放数据. 硬盘的内外部结构: 物理形状: 接口类型: IDE(I ...

  6. OK335xS pwm buzzer Linux driver hacking

    /**************************************************************************** * OK335xS pwm buzzer L ...

  7. Loj 114 k大异或和

    Loj 114 k大异或和 构造线性基时有所变化.试图构造一个线性基,使得从高到低位走,异或上一个非 \(0\) 的数,总能变大. 构造时让任意两个 \(bas\) 上有值的 \(i,j\) ,满足 ...

  8. fff

    https://qa.tutormeet.com/tutormeet/tutormeet_FF.html?lang=3&data=MjAxODAzMjcxODAwMTQ1OXwyNzQ2fGp ...

  9. ffmpeg V4L2_BUF_FLAG_ERROR的解决方法

    利用ffmpeg进行视频采集时经常出现“V4L2_BUF_FLAG_ERROR”的错误,并不再进行下帧的采集.通过借鉴下面的方法,对ffmpeg3.0.7版本进行补丁,能解决此类问题. 当某帧出错后, ...

  10. 两个不错点电影ED2000资源

    http://simplecd.me/ http://www.ed2000.com/ http://www.2tu.cc/ http://www.mp4ba.com/ http://www.ddyy. ...