博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-12-Integer to Roman
阅读量:6464 次
发布时间:2019-06-23

本文共 1842 字,大约阅读时间需要 6 分钟。

算法描述:

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       ValueI             1V             5X             10L             50C             100D             500M             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: 3Output: "III"

Example 2:

Input: 4Output: "IV"

Example 3:

Input: 9Output: "IX"

Example 4:

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

Example 5:

Input: 1994Output: "MCMXCIV"Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

解题思路:细节题,考虑数字之间的对应关系。

string intToRoman(int num) {        vector
ch = {
"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; vector
value = {
1,4,5,9,10,40,50,90,100,400,500,900,1000}; string results = ""; for(int i =value.size()-1; i >=0; i--){ while(num >= value[i]){ num -= value[i]; results += ch[i]; } } return results; }

 

转载于:https://www.cnblogs.com/nobodywang/p/10326626.html

你可能感兴趣的文章
HDU 4996 Revenge of LIS(DP)
查看>>
App里面如何正确显示用户头像
查看>>
DATAGUARD维护:从库宕机后如何恢复到管理恢复模式
查看>>
Android中的PID和UID
查看>>
U-BOOT之一:BootLoader 的概念与功能
查看>>
我的路上
查看>>
Velocity处理多余空白和多余空白行问题
查看>>
内容开发平台(PLATFORM)
查看>>
java值传递
查看>>
判断一个数是否为素数的一个讨论(一)
查看>>
DB2与oracle有什么区别
查看>>
创建一个多级文件目录
查看>>
Picasa生成图片幻灯片页面图文教程
查看>>
js获取当前时间的前一天/后一天
查看>>
[洛谷P3978][TJOI2015]概率论
查看>>
Python学习——编程语言介绍
查看>>
Python字符串的格式化
查看>>
C#反射---属性
查看>>
服务器常用的状态码及其对应的含义如下
查看>>
完美字符串
查看>>