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. boostrap-非常好用但是容易让人忽略的地方------modal

    使用bootstrap框架好久了,在开发中也用到了或者遇到了很多的问题,所以跟大家分享一下 bootstrap modal 组件的样式 .modal-lg .modal-sm 说明:这个是bootst ...

  2. Google大数据三篇著名论文----中文版

    Google File System中文版 Google Bigtable中文版      Google MapReduce中文版

  3. List toArrays()

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ListToArray ...

  4. .net程序调用检测和性能分析工具——DotTrace

    DotTrace可以对.net程序进行性能监测,对正在运行的程序和网站监控,主要界面如下: 需要将该工具安装在程序运行的服务器上. 主要用到这个视图,显示了每个方法的时间,下面是反编译出来的代码. P ...

  5. 《C++ Primer Plus》学习笔记6

    <C++ Primer Plus>学习笔记6 第11章 使用类 <<<<<<<<<<<<<<<&l ...

  6. Visual Studio Code开发TypeScript

    [Tool] 使用Visual Studio Code开发TypeScript   [Tool] 使用Visual Studio Code开发TypeScript 注意 依照本篇操作步骤实作,就可以在 ...

  7. find your present (2) 2095

    Problem Description In the new year party, everybody will get a "special present".Now it's ...

  8. HDU 4831 Scenic Popularity (段树)

    Scenic Popularity Problem Description 临近节日,度度熊们近期计划到室外游玩公园.公园内部包含了非常多的旅游景点区和歇息区,因为旅游景点非常热门,导致景点区和歇息区 ...

  9. php+flash头像上传组件

    有会员系统的站点一般都会有一个头像上传组件,一般做的最简单的是 这样的方式长处是代码写的简单,仅仅要推断图片大小和类型,然后更新数据库.可是用户体验不高.并且站点其它页面假设要使用较小的20X20或1 ...

  10. C#邮件收发

    public class Mail { private string mailkey; private string mailSecret; public string mailFrom; publi ...