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智能手表Galaxy Gear

    苹果定下来本月10日召开新品发布会,而它的竞争对手三星却抢先一步.今天凌晨,三星在德国柏林一口气发布了三款重量级产品.三星智能手表Galaxy Gear最引人关注,其将于9月25日陆续在全球上市,售价 ...

  2. SpringInAction--SpringMvc高级技术(servlet、filter、multipart)

    前面学了spirng的一些配置,以及web方面的知识,今天就在学习一下在spring比较常用的一些高级技术... 首先来介绍下什么叫servlet吧(来着维基百科) Servlet(Server Ap ...

  3. Alpha冲刺一 (6/10)

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10004107.html 作业博客:https://edu.cnblogs.com/campus ...

  4. ASP.NET FORM认证配置排错记录

    搞了2小时都不能实现自动跳转到登录页面,后删除了配置文件中的name,就解决问题了. <authorization>      <deny users="?" / ...

  5. 李氏滑动事件冲突解决方案 之 处理子ViewGroup的超棒方案

    父ViewGroup(CurView) 和 子 ViewGroup(ParentView) 滑动事件冲突解决方案 之 处理子ViewGroup的超棒方案: 子ViewGroup 以 SlipRelat ...

  6. C# #if, #else和#endif预处理指令

        #if 使您可以开始条件指令,测试一个或多个符号以查看它们是否计算为 true.如果它们的计算结果确实为true,则编译器将计算位于 #if 与最近的 #endif 指令之间的所有代码.例如, ...

  7. RabbitMQ部署

    操作步骤: 安装依赖文件 -->yum install ncurses-devel 进入opt目录创建rabbitmq目录 -->cd /opt -->mkdir rabbitMQ ...

  8. maven学习--基础篇

    2016-01-5 16:13:43 发现一些错误,文章修改中…… (部分图片截取自其他地方,不是很清楚) 一. maven的项目创建和基本命令 maven是一个项目管理工具,包含了一个项目对象模型P ...

  9. PHP如何清除COOKIE?PHP无法删除COOKIE?设置COOKIE有效期、COOKIE过期

    cookie和session的区别? http://www.cnblogs.com/phphuaibei/archive/2011/11/15/2250082.html PHP如何清除COOKIE?P ...

  10. Android SDK无法更新的问题解决办法

    问题: SSL hostname in certificate didn't matchhostname in certificate didn't match: <dl-ssl.google. ...