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的更多相关文章

  1. 【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 ...

  2. LeetCode(12)Integer to Roman

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

  3. LeetCodeOJ刷题之12【Integer to Roman】

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

  4. LeetCode第[13]题(Java):Roman to Integer

    题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  5. leetcode解决问题的方法||Integer to Roman问题

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

  6. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

  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. [LeetCode] 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][Python]Integer to Roman

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

随机推荐

  1. slf自己主动绑定实现类过程推断

    依照绑定实现类的方式是基于约定原则:推断分下面几个步骤 1.LoggerFactory扫描实现类路径有几个实现类,即在org/slf4j/impl/下有几个StaticLoggerBinder.cla ...

  2. Gallatin(大陆版)Office365中Exchange Online混合部署功能已经能够使用了

    经过測试,Exchange混合部署已经能够使用了 前置条件: 本机至少须要一台Exchange Server 2013作为混合部署server 须要一个公网域名 domian.com,能够和内部域名不 ...

  3. 减小Cookie体积

    HTTP coockie能够用于权限验证和个性化身份等多种用途.coockie内的有关信息是通过HTTP文件头来在webserver和浏览器之间进行交流的.因此保持coockie尽可能的小以降低用户的 ...

  4. SpringAccess数据库(oracle)构造

    陈科朝:http://blog.csdn.net/u013474104/article/details/44279309 ================ 1.spring 对数据库訪问的支持 当我们 ...

  5. 好记性不如烂笔头85-spring3学习(6)-BeanFactory 于bean生命周期

    假设BeanFactory为了产生.管理Bean, 一个Bean从成立到毁灭.它会经过几个阶段运行. 据我所知,一般bean包括在生命周期:设定,初始化,使用阶段,四个核心阶段销毁. 1.@Bean的 ...

  6. ViewPager用法

    第一图:          页面中填充内容是随机关键词飞入和飞出动画效果,随后会更新,如今请先无视吧 ---2015-02-27--- 两年后最终更新了,网上都能搜到的,哎 无奈太懒http://bl ...

  7. 基于PHP的crontab定时任务管理

    BY JENNER · 2014年11月10日· 阅读次数:6 linux的crontab一直是server运维.业务开展的利器.但当定时任务增多时,管理和迁移都变得非常麻烦,并且easy出问题.以下 ...

  8. LINUX2.4.x网络安全框架

       在分析LINUX2.4.x网络安全的实现之前先简介一下它里面包括的几个重要概念:netfilter.iptables.match.target.nf_sockopt_ops.网络安全功能点的实现 ...

  9. Android - 警告:it is always overridden by the value specified in the Gradle build script

    警告:it is always overridden by the value specified in the Gradle build script 本文地址: http://blog.csdn. ...

  10. 前端学习笔记(zepto或jquery)——对li标签的相关操作(一)

    对li标签的相关操作——点击li标签进行样式切换的两种方式 Demo演示: 1 2 3 4 // 详解: 第一种方式(以ul为基础): $("ul").bind("cli ...