当前位置:网站首页>12. Integer to Roman整数转罗马数字
12. Integer to Roman整数转罗马数字
2022-07-27 05:57:00 【DXB2021】
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
Symbol 字符 Value数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 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:
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I can be placed before V (5) and X (10) to make 4 and 9.
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X can be placed before L (50) and C (100) to make 40 and 90.
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C can be placed before D (500) and M (1000) to make 400 and 900.
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
Given an integer, convert it to a roman numeral.
给你一个整数,将其转为罗马数字。
Example 1:示例 1:
Input: num = 3 输入: num = 3
Output: "III" 输出: "III"
Explanation: 3 is represented as 3 ones.
Example 2:
Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 3:
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
示例 2:
输入: num = 4
输出: "IV"
示例 3:
输入: num = 9
输出: "IX"
示例 4:
输入: num = 58
输出: "LVIII"
解释: L = 50, V = 5, III = 3.
示例 5:
输入: num = 1994
输出: "MCMXCIV"
解释: M = 1000, CM = 900, XC = 90, IV = 4.
Constraints:提示:
1 <= num <= 3999
C语言(超出时间限制)
char * intToRoman(int num){
char Roman[16];
int i=0,temp;
while(num)
{
if(num/1000>0)
{
temp=num/1000;
while(temp)
{
if(temp>0&&temp<4)
{
Roman[i++]='M';
temp--;
}
}
if(temp==0)
{
num=num%1000;
continue;
}
}
if(num/100>0)
{
temp=num/100;
while(temp)
{
if(temp==9)
{
Roman[i++]='C';
Roman[i++]='M';
temp=temp-9;
}
else if(temp>4&&temp<9)
{
Roman[i++]='D';
while(temp>5)
{
Roman[i++]='C';
temp--;
}
temp=temp-5;
}
else if(temp==4)
{
Roman[i++]='C';
Roman[i++]='D';
temp=temp-4;
}
else if(temp>0&&temp<4)
{
Roman[i++]='C';
temp--;
}
}
if(temp==0)
{
num=num%100;
continue;
}
}
if(num/10>0)
{
temp=num/10;
while(temp)
{
if(temp==9)
{
Roman[i++]='X';
Roman[i++]='C';
temp=temp-9;
}
else if(temp>4&&temp<9)
{
Roman[i++]='L';
while(temp>5)
{
Roman[i++]='X';
temp--;
}
temp=temp-5;
}
else if(temp==4)
{
Roman[i++]='X';
Roman[i++]='L';
temp=temp-4;
}
else if(temp>0&&temp<4)
{
Roman[i++]='X';
temp--;
}
}
if(temp==0)
{
num=num%10;
continue;
}
}
if(num>0)
{
while(num)
{
if(num==9)
{
Roman[i++]='I';
Roman[i++]='X';
temp=temp-9;
}
else if(temp>4&&temp<9)
{
Roman[i++]='V';
while(temp>5)
{
Roman[i++]='X';
temp--;
}
temp=temp-5;
}
else if(temp==4)
{
Roman[i++]='I';
Roman[i++]='V';
temp=temp-4;
}
else if(temp>0&&temp<4)
{
Roman[i++]='I';
temp--;
}
}
if(temp==0)
{
continue;
}
}
}
return Roman;
}C语言:
char * intToRoman(int num){
char* ans=(char*)malloc(16*sizeof(char));
int cur=0;
while(num>=1000)
{
ans[cur++]='M';
num-=1000;
}
if(num>=900)
{
ans[cur++]='C';
ans[cur++]='M';
num-=900;
}
else if(num>=500)
{
ans[cur++]='D';
num-=500;
}
else if(num>=400)
{
ans[cur++]='C';
ans[cur++]='D';
num-=400;
}
while(num>=100)
{
ans[cur++]='C';
num-=100;
}
if(num>=90)
{
ans[cur++]='X';
ans[cur++]='C';
num-=90;
}
else if(num>=50)
{
ans[cur++]='L';
num-=50;
}
else if(num>=40)
{
ans[cur++]='X';
ans[cur++]='L';
num-=40;
}
while(num>=10)
{
ans[cur++]='X';
num-=10;
}
if(num>=9)
{
ans[cur++]='I';
ans[cur++]='X';
num-=9;
}
else if(num>=5)
{
ans[cur++]='V';
num-=5;
}
else if(num>=4)
{
ans[cur++]='I';
ans[cur++]='V';
num-=4;
}
while(num>=1)
{
ans[cur++]='I';
num--;
}
ans[cur]='\0';
return ans;
}执行结果:通过
执行用时:8 ms, 在所有 C 提交中击败了37.27%的用户
内存消耗:5.7 MB, 在所有 C 提交中击败了83.05%的用户
通过测试用例:3999 / 3999
边栏推荐
- Interpretation of deepsort source code (V)
- Interpretation of deepsort source code (VII)
- Go obtains the processing results of goroutine through channel
- 网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...
- Gbase 8C technical features
- PHP defines the array using commas,
- 仿真模型简单介绍
- 36 - 新的 Promise 方法:allSettled & any & race
- Why can cross entropy loss be used to characterize loss
- Codeforces Round #804 (Div. 2)(5/5)
猜你喜欢

Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort

DDD Domain Driven Design Notes

Pytorch uses data_ Prefetcher improves data reading speed

nvidia-smi 各参数意义

DNA (deoxyribonucleic acid) supply | carbon nanotube nucleic acid loaded dna/rna material | dna/rna nucleic acid modified magnetic nanoparticles

ESP8266(ESP-12F) 第三方库使用 -- SparkFun_APDS9960 (手势识别)

Basic statement of MySQL (1) - add, delete, modify and query

Visual horizontal topic bug1:filenotfounderror: could not find module 'mvcameracontrol dll‘ (or one of it

Summary of APP launch in vivo application market

(转帖)eureka、consul、nacos的对比2
随机推荐
使用pip命令切换不同的镜像源
基于SSM学生学籍管理系统
Codeworks round 809 (Div. 2) (6/6) (Kruskal reconstruction tree)
MySQL limit paging query optimization practice
Interpretation of deepsort source code (V)
Gbase 8C technical features
C time related operation
把Excel转换成CSV/CSV UTF-8
端口转发小结
Interpretation of deepsort source code (III)
Digital image processing Chapter 1 Introduction
Codeforces Round #787 (Div. 3)(7/7)
Student status management system based on SSM
Details of cross entropy loss function in pytorch
DDD Domain Driven Design Notes
Shell programming specifications and variables
Bert and RESNET can also be trained on mobile phones?!
从技术原理看元宇宙的可能性:Omniverse如何“造”火星
算法--斐波那契数列(Kotlin)
Analysis of online and offline integration mode of o2o E-commerce