12. Integer to Roman
Medium

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

Example 1:

Input: 3
Output: "III"

Example 2:

Input: 4
Output: "IV"

Example 3:

Input: 9
Output: "IX"

Example 4:

Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 5:

Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
 class Solution {
public:
string bit(int i, int id){
string s;
if(id == ){
if(i==) s = "M";
else if(i==) s = "MM";
else if(i==) s = "MMM";
}
else if(id==){
if(i==) s = "C";
else if(i==) s = "CC";
else if(i==) s = "CCC";
else if(i==) s = "CD";
else if(i==) s = "D";
else if(i==) s = "DC";
else if(i==) s = "DCC";
else if(i==) s = "DCCC";
else if(i==) s = "CM";
else s = "";
}
else if(id==){
if(i==) s = "X";
else if(i==) s = "XX";
else if(i==) s = "XXX";
else if(i==) s = "XL";
else if(i==) s = "L";
else if(i==) s = "LX";
else if(i==) s = "LXX";
else if(i==) s = "LXXX";
else if(i==) s = "XC";
else s = "";
}
else if(id==){
if(i==) s = "I";
else if(i==) s = "II";
else if(i==) s = "III";
else if(i==) s = "IV";
else if(i==) s = "V";
else if(i==) s = "VI";
else if(i==) s = "VII";
else if(i==) s = "VIII";
else if(i==) s = "IX";
else s = "";
}
return s;
}
string intToRoman(int num) {
string ans;
ans += bit(num/,);
num %=;
ans += bit(num/,);
num %=;
ans += bit(num/,);
num %=;
ans += bit(num,);
return ans;
} };

Leetcode 12. Integer to Roman(打表,水)的更多相关文章

  1. Leetcode 12——Integer to Roman

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

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

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

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

  5. Java [leetcode 12] Integer to Roman

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

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

  7. LeetCode——12. Integer to Roman

    一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...

  8. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

  9. [leetcode] 12. Integer to Roman

    关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...

随机推荐

  1. Canvas入门02-绘制直线

    主要使用的API有: context.moveTo(x,y) 声明线的起始坐标 context.lineTo(x,y) 声明线的下一个坐标 context.fillStyle  声明线的填充颜色 co ...

  2. 十、Zabbix-自动关联模板

    之前的文章中,我们实现了自动注册,自动分组:并且创建了模板,监控项,触发器.为的就是能够实现主机自动被期望的监控项监控到.接下来我们只要能让自动注册的主机能够自动连接到我们设置好的模板,就可以实现自动 ...

  3. BAT推荐免费下载JAVA转型大数据开发全链路教程(视频+源码)价值19880元

    如今随着环境的改变,物联网.AI.大数据.人工智能等,是未来的大趋势,而大数据是这些基石,万物互联,机器学习都是大数据应用场景! 为什么要学习大数据?我们JAVA到底要不要转型大数据? 好比问一个程序 ...

  4. AcWing 875. 快速幂

    题目链接:https://www.acwing.com/problem/content/description/877/ 快速幂模板题,计算ab mod p 的值,a,b,p大概1e9左右,可以快速计 ...

  5. bfs(同一最短路径)

    http://oj.jxust.edu.cn/contest/Problem?id=1702&pid=7 题意:现在有两个相同大小的地图,左上角为起点,右下角问终点.问是否存在同一条最短路径. ...

  6. 常用php算法

       一.冒泡排序function bubble($array){ $cnt = count($array); if($cnt <= 0) return $array; for($i =1;$i ...

  7. Javascript高级面试

    原型 异步 一.什么是单线程,和异步有什么关系 单线程:只有一个线程,同一时间只能做一件事原因:避免DOM渲染的冲突解决方案:异步 为什么js只有一个线程:避免DOM渲染冲突 浏览器需要渲染DOM J ...

  8. __init__ 和__new__的区别?

    init 在对象创建后,对对象进行初始化. new 是在对象创建之前创建一个对象,并将该对象返回给 init.

  9. hdu1263 简单模拟

    题意:依据水果销量表.依照特定格式输出 格式:首先按产地排序,然后同一产地按水果名排序 注意:第一,设计多级排序           第二.同一产地同一水果可能多次出现,所以须要在前面已经输入的水果里 ...

  10. offsetWidth clientWidth scrollWidth 的区别

    了解 offsetWidth clientWidth scrollWidth 的区别 最近需要清除区分开元素的width,height及相应的坐标等,当前这篇用来区分offsetWidth clien ...