Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

将整数用罗马数字表示。

思路分析:  

{"","I","II","III","IV","V","VI","VII","VIII","IX"},
{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
{"","M","MM","MMM"}

    首先从输入的整数开始,(1)取其个位上的数值,判定后得到一个字符串(这个字符串也应该位于罗马数字的最右边,所以再用一个栈来解决)

               (2)然后将整数除以10(轮到十位上的数值了),再去执行(1)

代码如下:

    

  1. #include<stack>
  2. class Solution {
  3. public:
  4. string intToRoman(int num) {
  5. vector<vector<string>> data={
  6. {"","I","II","III","IV","V","VI","VII","VIII","IX"},
  7. {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
  8. {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
  9. {"","M","MM","MMM"}
  10. };
  11. stack<string> sta;
  12. int i=;
  13. string res="",result="";
  14. while(num!=){
  15. int remain=num%;
  16. res=data[i][remain];
  17. sta.push(res);
  18. ++i;
  19. num/=;
  20. }
  21. while(!sta.empty()){
  22. result+=sta.top();
  23. sta.pop();
  24. }
  25. return result;
  26. }
  27. };

integer to roman leetcode c++实现的更多相关文章

  1. Integer to Roman - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Integer to Roman - LeetCode 注意点 考虑输入为0的情况 解法 解法一:从大到小考虑1000,900,500,400,100,9 ...

  2. Integer to Roman -- LeetCode 012

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

  3. Integer To Roman leetcode java

    问题描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  4. [LeetCode][Python]Integer to Roman

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

  5. leetCode练题——12. Integer to Roman

    1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C,  ...

  6. 【LeetCode】Roman to Integer & Integer to Roman

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

  7. 【leetcode】Integer to Roman

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

  8. 【leetcode】Integer to Roman & Roman to Integer(easy)

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

  9. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

随机推荐

  1. 【旧文章搬运】Idle进程相关的一些东西

    原文发表于百度空间,2009-05-13========================================================================== Idle进 ...

  2. Unity脚本打包android工程

    using UnityEngine; using System.Collections; using UnityEditor; public class NewBehaviourScript : Ed ...

  3. IT兄弟连 JavaWeb教程 重定向

    HTTP协议规定了一种重定向机制,重定向的运作流程如下: ●  用户在浏览器端输入特定URL,请求访问服务器端的某个组件. ●  服务器端的组件返回一个状态码为302的响应结果,该响应结果的含义为: ...

  4. mac的日常使用总结

    目录 有一个github的仓库:(强烈推荐) 不推荐的但是可以试试的一些链接: # 关于mac book的使用教程 github简直是一个宝藏,发现好多各种好玩的东西, 爱了爱了, 开源一定是未来, ...

  5. locale localedef --之Linux字符集理解

    参考: https://www.cnblogs.com/dolphi/p/3622420.html     http://www.360doc.com/content/15/1105/08/14513 ...

  6. 改变UITabbar顶部分割线颜色

    项目中是使用UITabbarController 因此改变UITabbar的分割线代码如下 由于美术没提供图片,所以自己创建了个图片 //改变tabbar 线条颜色 CGRect rect = CGR ...

  7. iOS 上传的图片在HTML上显示时,图片方向信息(EXIF Orientation)异常

    将iPhone 6s拍摄的照片上传到服务器之后, 在Web网页上看到图片被逆时针旋转了90度, 这让我很惆怅呐! 出现这个问题其实是因为上传的图片为.jpg格式,.jpg文件含有EXIF信息, 其中E ...

  8. 通过 xshell 连接 ubuntu on windows(WSL)

    装上 ubuntu on windows 后,默认要先打开 cmd, 再运行 bash 进入 ubuntu 的 shell. 但是这个shell很难看,配色不好就算了,还存在各种复制粘贴麻烦. 默认没 ...

  9. Jamie and Binary Sequence (changed after round) - CodeForces 916B

    http://codeforces.com/problemset/problem/916/B 好尬啊... #include<cstdio> #include<algorithm&g ...

  10. iOS中自定义UITableViewCell的用法

    1.先创建一个View继承 UITableViewCell并使用xib快速建立模型. #import <UIKit/UIKit.h> #import "Score.h" ...