首页 > 其他 > 详细

Leetcode 12 Integer to Roman整数变罗马

时间:2015-04-22 13:53:30      阅读:211      评论:0      收藏:0      [点我收藏+]
题目:

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;		
	}

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

Leetcode 12 Integer to Roman整数变罗马

原文:http://blog.csdn.net/vvaaiinn/article/details/45193631

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!