给定一个整数,将其转为罗马数字。输入保证在 1 到 3999 之间。

详见:https://leetcode.com/problems/integer-to-roman/description/

class Solution {
public:
string intToRoman(int num) {
string res = "";
char roman[] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
int value[] = { 1000, 500, 100, 50, 10, 5, 1 }; for (int n = 0; n < 7; n += 2)
{
int x = num / value[n];
if(x<4)
{
for(int i=1;i<=x;++i)
{
res+=roman[n];
}
}
else if(x==4)
{
res=res+roman[n]+roman[n-1];
}
else if(x>4&&x<9)
{
res+=roman[n-1];
for(int i=6;i<=x;++i)
{
res+=roman[n];
}
}
else if (x == 9)
{
res=res+roman[n]+roman[n-2];
}
num%=value[n];
}
return res;
}
};

参考:http://www.cnblogs.com/grandyang/p/4123374.html

012 Integer to Roman 整数转换成罗马数字的更多相关文章

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

  2. [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 ...

  3. [LintCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...

  4. [Leetcode] Interger to roman 整数转成罗马数字

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

  5. lintcode :Integer to Roman 整数转罗马数字

    题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...

  6. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

  7. 将整数转换成二进制的java小程序

    首先我们知道,将整数转换成二进制是将整数除二取余将最后除得的数和得到的余数从下向上写,组成得到的二进制数. java程序实现如下: public class ChangeToErjinzhi { pu ...

  8. Javascript里,想把一个整数转换成字符串,字符串长度为2

    Javascript里,想把一个整数转换成字符串,字符串长度为2.  想把一个整数转换成字符串,字符串长度为2,怎么弄?比如 1 => "01"11 => " ...

  9. int类型的整数转换成汉字

    int类型的整数转换成汉字 一.源代码:IntegerNumberToChinese.java package cn.com.zfc.example; import java.util.Scanner ...

随机推荐

  1. bzoj 3144 切糕 —— 最小割

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3144 每个点拆成 R 个,连成一条链,边上是权值,割掉代表选这一层: 然后每个点的第 t 层 ...

  2. jumpserver遇到的坑

    安装:https://github.com/jumpserver/jumpserver,看readme照着做就行,下面是遇到的坑.   0.4.4版坑: 1.要升级pip,否则有的包装不上   2.p ...

  3. select元素选择时间以及jQuery对select的属性操作

    <select class="input04" id="1" name="in_class1" onchange="setc ...

  4. 通过pip3安装ipython

    操作系统:Centos7.4:ipython可以用来测试python语句,故需要安装. 首先需要安装epelde的扩展库: easy_install是由PEAK(Python Enterprise A ...

  5. Ruby中的include

    Ruby中的include语句应注意以下两个问题: 1.include与文件无关.C语言中,#include预处理指令在编译期将一个文件的内容插入到另一个文件中.Ruby语句只是简单地产生一个指向指定 ...

  6. JavaScript跳转和打开新窗口

    跳转: window.location.href = "www.baidu.com"  // 跳转到百度首页,不打开新的浏览器窗口 等价于html中的<a href=&quo ...

  7. django examples 学习笔记(1)创建一个独立的python环境

    pip install virtualenv   创建一个虚拟环境 virtualenv   my_env      创建一个独立的环境 source my_env/bin/activate   激活 ...

  8. 线程中event.wait() event_obj.set() 的使用

    #!/usr/bin/env python import threading # event.wait()##阻断线程向下执行 event_obj.set()#释放进程向下执行 def do(even ...

  9. 转:Linux下用Jmeter做接口测试

    本地设计 首先在本地设计 Apache JMeter 测试计划,大家可以参考<接口测试之 JMeter 初探> ,这里不再重复. 服务器配置 确保服务器已经安装了JDK和Python. 在 ...

  10. sklearn有关参数

    from sklearn import datasets from sklearn.linear_model import LinearRegression import matplotlib.pyp ...