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
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 beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(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(打表,水)的更多相关文章
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- [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 ...
- [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 ...
- [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 ...
- Java [leetcode 12] Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- [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 ...
- LeetCode——12. Integer to Roman
一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
- [leetcode] 12. Integer to Roman
关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...
随机推荐
- EF Core 初始化数据库的两种方法。
使用DbContextSeed初始化数据库 添加链接字符串 // This method gets called by the runtime. Use this method to add serv ...
- 剑指Offer编程题(Java实现)——复杂链表的复制
题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...
- uboot 主Makefile分析
一. Makefile 配置 1.1. make xxx_config 1.1.1. 笔者实验时是make x210_sd_config a. x210_sd_config是Makefile下的一个目 ...
- 批处理遍历文件夹执行git pull
echo off & color 0A for /d %%f in (D:\www\*) do ( D: cd %%f chdir git pull ) pause 遍历D:\www\这个文件 ...
- 【已解决】Error running 'xxx项目' Command line is too long(idea版)
[错误] Error running 'xxx项目': Command line is too long. Shorten command line for xxx or also for Sprin ...
- 剑指offer-二维数组中的查找-数组-python
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- 利用中转输出表制作HijackDll
[原创]利用中转输出表制作HijackDll(附工具源码)作 者: baixinye时 间: 2012-08-05,16:48:45链 接: http://bbs.pediy.com/showthre ...
- vue生命周期简单总结
生命周期(钩子函数):一个组件从创建到销毁的过程就是生命周期 beforeCreate: 创建前 1.当前vue实例化的时候会做一个初始化的操作,在这个生命周期函数中我们可以做初始化的 ...
- maven多模块tomcat启动报 NoClassDefFoundError:com/test/main/message
maven多模块tomcat启动报 NoClassDefFoundError:com/test/main/message 扫描不到 添加子模块jar包
- 007-流程控制 if 语句
流程控制 if 语句 if [ 条件判断式 ] ; then 程序 fi if [ 条件判断式 ] then 程序 fi 脚本示例: [root@zabbix lianxi]# .sh #!/bin/ ...