当前位置:网站首页>Daily question - Roman numeral to integer
Daily question - Roman numeral to integer
2022-06-11 21:57:00 【Code loving students】
Title Description :
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 ,
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.
title :
1. When a small number is to the left of a large number, it means a large number - Small numbers
2. When a large number is on the right, it means a large number + Small numbers
Then through these two laws , We can find out when Array s[i] <s[I+1] when , We subtract s[i], If greater than, add s[i].
Code implementation :
int GetValue(char s){
int value;
switch(s){
case 'I':value = 1; break;
case 'V':value = 5; break;
case 'X':value = 10; break;
case 'L':value = 50; break;
case 'C':value = 100; break;
case 'D':value = 500; break;
case 'M':value = 1000; break;
}
return value;
}
int romanToInt(char* s) {
int count = 0;
int n = strlen(s);// Find the length of the incoming character
int i,temp;
for (int i = 0; i < n; ++i) {
temp = GetValue(s[i]);
if (i < n - 1 && (temp < GetValue(s[i + 1]))){
count -= temp;
}
else {
count += temp;
}
}
return count;
}
Be careful : 1. utilize switch It can save the length of code .
2. i Must satisfy less than n-1, Otherwise, when s[i+1] Execution time , Will cross the border to visit .
Finally back to count Value can complete the requirements of the topic .
边栏推荐
- 为什么需要微服务
- LaTex实战笔记 3-宏包与控制命令
- JVM | runtime data area; Program counter (PC register);
- 判断大小端存储两种办法
- The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
- crontab中定时执行shell脚本
- In the post epidemic era, how can enterprise CIOs improve enterprise production efficiency through distance
- 类与对象(3)
- Classes and objects (3)
- 超标量处理器设计 姚永斌 第2章 Cache --2.4 小节摘录
猜你喜欢

Example of using zypper command

The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace

Leetcode-43- string multiplication

Matlab: 文件夹锁定问题的解决

C语言实现迷宫问题

ESP32C3 Arduino库使用方法

Leetcode-322- change exchange
![[niuke.com] dp31 [template] complete Backpack](/img/81/5f35a58c48f05a5b4b6bdc106f5da0.jpg)
[niuke.com] dp31 [template] complete Backpack

R语言相关文章、文献整理合集(持续更新)

实现栈和队列
随机推荐
How to view the installation date of the win system
Redis transaction
實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation
Redis basic data type (set)
Leetcode-104- maximum depth of binary tree
Building a custom CNN model: identifying covid-19
Why microservices are needed
RPA+低代码助推品牌电商启新创变、重启增长
R language book learning 03 "in simple terms R language data analysis" - Chapter 10 association rules Chapter 11 random forest
go encoding包
EndnoteX9简介及基本教程使用说明
Dynamic memory management (1)
The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success
联调这夜,我把同事打了...
2022-02-28(2)
类和对象(2)
动态内存管理(1)
为什么需要微服务
Custom implementation offsetof