Leetcode 12 Integer to Roman整数变罗马

题目:

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

翻译:

给一个整数,把他转换为罗马数字输出。

这道题是简单的字符转换的问题。只要了解罗马数字代表的含义即可。

罗马数字里面只有 1,5,10.  1到3之间用重复的1表示,4用IV 90用XC 依次类推。

I = 1;

V = 5;

X = 10;

L = 50;

C = 100;

D = 500;

M = 1000;

代码:

	public static String intToRoman(int num)
	{
		String str ="";
		String roman[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
		int number[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
		for(int i = 0 ;num!=0 ;i++)
		{
			while(num >= number[i])
			{
				num-=number[i];
				str+=roman[i];
			}
		}
		return str;
	}

这道题比较简单。只要了解罗马数字代表的 意思即可。

时间: 2024-07-31 21:36:38

Leetcode 12 Integer to Roman整数变罗马的相关文章

[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 i

leetCode 12. Integer to Roman | 字符串 | Medium

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题目大意: 将一个给定的阿拉伯数字转换成罗马数字. 思路: 这题看到的时候,想的太多. 其实很简单,将千位,百位,十位,个位都表示出来,然后组合即可. 代码如下: class Solution { public:     string int

[LeetCode][12]Integer to Roman解析 int转罗马字符时间复杂度为常数的实现-Java实现

Q: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. A: 俺是真不知道啥是roman,我去查了一下合着是罗马字体,这题目意思也很简单就是说我要输入一个数字可以保证是1-3999但是你要把这个阿拉伯数字转换成罗马数字. 首先我们要清楚罗马数字怎么表示的,罗马数字表示如下: 罗 马 数字共有七个,即I(1),V(5),X(10),L

LeetCode 12 Integer to Roman(整型数到罗马数)

翻译 给定一个整型数值,将其转换到罗马数字. 输入被保证在1到3999之间. 原文 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 我不会告诉你一开始我是用的无数个变量和if-- 后来实在受不了这么多变量就将其写成了枚举,那么接下来就迎刃而解了. 为了让大家理解罗马数是怎么计数的,这里我截了一张图,具体的大家可以自行用微软Bing

LeetCode 12 Integer to Roman (整数转罗马数字)

题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", "M", "MM", "MMM”};//1000~3000String C[] = {"", "C", "CC", "CCC", "CD", "D&quo

[LeetCode] 12. Integer to Roman ☆☆

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 解释: 罗马数字采用七个罗马字母作数字.即Ⅰ(1).X(10).C(100).M(1000).V(5).L(50).D(500).记数的方法: 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3: 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8.

LeetCode 12 Integer to Roman(C,C++,Java,Python)

Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Solution: 根据数字将每一位转换为罗马字符串即可,时间复杂度O(len(num)) 题目大意: 给一个整数,将整数调整为罗马数字,关于罗马数字的定义见这里:罗马数字 个位数举例 Ⅰ,1 ]Ⅱ,2] Ⅲ,3] Ⅳ,4 ]Ⅴ,5 ]Ⅵ,6]Ⅶ,7] Ⅷ,8 ]Ⅸ

LeetCode 12 integer to roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999 与罗马字符转数字类似,注意按级处理. string intToRoman(int num) { string sRet; int iNum = num; while(iNum > 0) { if(iNum < 5) { if(iNum == 4) { sRet += "IV

12. Integer to Roman 整数转罗马数字

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Solution:     def intToRoman(self, num):         """