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. Bitdefender Total Security 2014 Free 6 Months & 12 month License Key

    German Only – Bitdefender Total Security 2014 Free 6 Months Serial License Keyhttp://www.bitdefender ...

  2. Try Ubuntu Landscape on Xenial (by quqi99)

    版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99) Landscape is used for mana ...

  3. 博客(第0次作业)—— New Starting Point

    一.最理想的师生关系是健身教练和学员的关系,在这种关系中你期望获得来自老师的那些帮助? 正如文章中所说,这些学员的想法得足够强烈, 他/她才会花钱去参加这样的健身活动,每一个来学习的学生,  都是想学 ...

  4. (译)KVO的内部实现

    09年的一篇文章,比较深入地阐述了KVO的内部实现.   KVO是实现Cocoa Bindings的基础,它提供了一种方法,当某个属性改变时,相应的objects会被通知到.在其他语言中,这种观察者模 ...

  5. 从event loop规范探究javaScript异步及浏览器更新渲染时机

    异步的思考 event loops隐藏得比较深,很多人对它很陌生.但提起异步,相信每个人都知道.异步背后的“靠山”就是event loops.这里的异步准确的说应该叫浏览器的event loops或者 ...

  6. ppt修改默认字体

      首先,在文本框中输入文字,选中文字设置为自己需要的效果,比如文字字体设置为微软雅黑,大小设置为24,颜色设置为水绿色.   鼠标移动到到输入文本框的边上,此时鼠标形状会变成十字形,单击右键,在弹出 ...

  7. 【java多线程】java的线程池分析

    (一)线程池的拒绝策略 --->拒绝策略的接口java.util.concurrent.RejectedExecutionHandler --->终止策略(默认):java.util.co ...

  8. 深入理解Java虚拟机,gc输出参数

    https://blog.csdn.net/qq_21383435/article/details/80702205

  9. Oracle.DataAccess.dll 部署安装

    Oracle.DataAccess.dll 要拷贝到项目发布目录 项目发布的时候,还必须要拷贝以下几个文件在运行目录1.oci.dll 2.oraociicus11.dll 3.OraOps11w.d ...

  10. Bootstrap-Plugin:下拉菜单(Dropdown)插件

    ylbtech-Bootstrap-Plugin:下拉菜单(Dropdown)插件 1.返回顶部 1. Bootstrap 下拉菜单(Dropdown)插件 Bootstrap 下拉菜单 这一章讲解了 ...