leetcode第12题--Integer to Roman
Problem:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
把阿拉伯数字转换为罗马数字输出。百度一下对应的 I V X L C D M,代表1,5,10,50,100,500,1000 然后写一个子函数,输入数字和相应的位数级别,如个位为level 1,千为4.因为最多不会超过四千。所以可以如下。注意了,string用法很好,直接加就可以。
class Solution {
private:
string corRoman(int val, int level)
{
string base, media, large;
string s = "";
if (level == ) // 个位
{
base = "I";
media = "V";
large = "X";
}
else if (level == )
{
base = "X";
media = "L";
large = "C";
}
else if (level == )
{
base = "C";
media = "D";
large = "M";
}
else
{
base = "M";
}
if (val == )
return "";
if( val < )
{
for ( int i = ; i < val; i++)
s += base;
return s;
}
if (val == )
{
return base + media;
}
if (val < )
{
s = media;
for (int i = ; i < val; i++)
s+=base;
return s;
}
return base + large;
}
public:
string intToRoman(int num)
{
string s;
int a;
for (int i = ; i > ; i-- )
{
a = num/pow(,i - );
num %= (int)pow(, i - );
s+=corRoman(a,i);
}
return s;
}
};
这样就Accept了。
leetcode第12题--Integer to Roman的更多相关文章
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- LeetCode(12)Integer to Roman
题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- LeetCodeOJ刷题之12【Integer to Roman】
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- LeetCode第[13]题(Java):Roman to Integer
题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- leetcode解决问题的方法||Integer to Roman问题
problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- [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 ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
随机推荐
- JSP简单的练习-功能标签
<!-- userfn.jsp --> <%@ page contentType="text/html;charset=gb2312" %> <%@ ...
- mousewheel 与 DOMMouseScroll
FF使用DOMMouseScroll,其他浏览器使用mousewheel FF在一个特殊的属性event.detail.表示滚动的值 event.detail 正数:向下滚动,负数:向上滚动 滚动一次 ...
- ViewPager实现页面切换
先贴效果图(每个开关Tab债券.尾随页变化.效果图蓝条添加的用户体验) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/fo ...
- WebService之CXF注解报错(一)
WebService之CXF注解 1.详细报错例如以下 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] ...
- WIN8 、WIN7 下IIS7.5、IIS8 的rewrite 伪静态功能设置方法
原文 WIN8 .WIN7 下IIS7.5.IIS8 的rewrite 伪静态功能设置方法 win7和win8系统都自带有iis的功能.关于IIS的安装,上一篇已经讲述,这里就不重复了. 下面说下在w ...
- Scala刮:使用Intellij IDEA写hello world
介绍 在前面的文章中,,我们介绍了如何使用Scala IDE那是,eclipse集成Scala开发插件Scala开发语言程序.使用一段时间后,.发现eclipse正确Scala支持不是很好.用户体验差 ...
- SQL Server 备份和还原
SQL Server 备份和还原 SQL Server 备份 恢复模式 SQL Server 数据恢复模式分为三种:完整恢复模式.大容量日志恢复模式.简单恢复模式. 完整恢复模式 默认的恢复模式, ...
- js中document.write()使用方法
<script> var hrf = window.location.href; if(hrf.indexOf('change')>0){ ...
- Unity+NGUI打造网络图片异步加载和本地缓存工具(一)
我们已经开发了在移动终端中,异步网络图片被装入多,在unity其中尽管AssetBundle存在,通常第一个好游戏的资源,然后加载到现场,但也有很多地方可以使用异步网络加载图像以及其缓存机制. 我也写 ...
- Canvas旋转图片--保持相同大小的算法
function drawImg(angle) { canvas.width = canvas.width; var distance = size / 2 * Math.sqrt(2) ...