当前位置:网站首页>12. integer to Roman numeral
12. integer to Roman numeral
2022-06-11 07:01:00 【Not coriander】
Roman numerals contain the following seven characters : I, V, X, L,C,D and M.
character The number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
for example , Rome digital 2 Write to do II , Two parallel 1.12 Write to do XII , That is to say X + II . 27 Write to do XXVII, That is to say XX + V + II .
Usually , The small numbers in roman numbers are to the right of the big ones . But there are special cases , for example 4 Do not write IIII, It is IV. Numbers 1 In number 5 Left side , The number represented is equal to the large number 5 Decimal reduction 1 Value obtained 4 . similarly , Numbers 9 Expressed as IX. This special rule only applies to the following six cases :
I Can be placed in V (5) and X (10) Left side , To express 4 and 9.
X Can be placed in L (50) and C (100) Left side , To express 40 and 90.
C Can be placed in D (500) and M (1000) Left side , To express 400 and 900.
Give you an integer , Turn it into Roman numerals .
Example 1:
Input : num = 3
Output : "III"
Example 2:
Input : num = 4
Output : "IV"
Example 3:
Input : num = 9
Output : "IX"
Example 4:
Input : num = 58
Output : "LVIII"
explain : L = 50, V = 5, III = 3.
Example 5:
Input : num = 1994
Output : "MCMXCIV"
explain : M = 1000, CM = 900, XC = 90, IV = 4.
Tips :
1 <= num <= 3999
1. enumeration
class Solution {
public:
string intToRoman(int num) {
string str="";
while(num){
if(num>=1000){
num-=1000;
str+="M";
}
else if(num>=900){
num-=900;
str+="CM";
}
else if(num>=500){
num-=500;
str+="D";
}
else if(num>=400){
num-=400;
str+="CD";
}
else if(num>=100){
num-=100;
str+="C";
}
else if(num>=90){
num-=90;
str+="XC";
}
else if(num>=50){
num-=50;
str+="L";
}
else if(num>=40){
num-=40;
str+="XL";
}
else if(num>=10){
num-=10;
str+="X";
}
else if(num>=9){
num-=9;
str+="IX";
}
else if(num>=5){
num-=5;
str+="V";
}
else if(num>=4){
num-=4;
str+="IV";
}
else if(num>=1){
num-=1;
str+="I";
}
}
return str;
}
};
2. greedy
class Solution {
public:
string intToRoman(int num) {
int values[13]={
1000,900,500,400,100,90,50,40,10,9,5,4,1};
string rom[13]={
"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
string str="";
for(int i=0;i<13;i++){
while(num>=values[i]){
str+=rom[i];
num-=values[i];
}
}
return str;
}
};
边栏推荐
- Promise details
- RGB-D Salient Object Detection withCross-Modality Modulation and Selection
- Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
- Pytest automated test - easy tutorial (01)
- [并发进阶]——线程池总结
- Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
- 【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
- About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally
- JVM from getting started to giving up 2: garbage collection
- Whether the ZABBIX monitoring host is online
猜你喜欢

LEARNING TARGET-ORIENTED DUAL ATTENTION FOR ROBUST RGB-T TRACKING

洛谷P1091合唱队形(最长上升子序列)
![[deploy private warehouse based on harbor] 3 deploy harbor](/img/cd/be68a430e86b4b23ad93b42a338f00.jpg)
[deploy private warehouse based on harbor] 3 deploy harbor

Shutter restraint container assembly

【迅为干货】龙芯2k1000开发板opencv 测试

ESP32学习笔记(49)——ESP-WIFI-MESH接口使用

Leetcode hot topic 100 topic 11-15 solution

Transformer Tracking

Flutter 约束容器组件

JVM from getting started to abandoning 1: memory model
随机推荐
1266_FreeRTOS调度器启动代码实现分析
A highly controversial issue
About daily report plan
The difference between arrow function and ordinary function
开源漫画服务器Mango
client-go gin的简单整合六-list-watch二(关于Rs与Pod以及Deployment的完善)
Appclips & tips (continuous update)
农房一体脚本的心得记录
Notice on organizing the application for the first edition of Ningbo key software in 2022
June training (day 11) - matrix
Common troubleshooting tools and analysis artifacts are worth collecting
【迅为干货】龙芯2k1000开发板opencv 测试
Oracle prompt invalid number
First day of database
Array information management system reconfiguration preheating (1) how to write basic logic using linear continuous structure?
Leetcode-647.Palindromic Substrings
This comprehensive understanding
VTK vtkplane and vtkcutter use
JVM from getting started to abandoning 1: memory model
Common modules of saltstack