Integer to Roman【LeetCode】

来源:互联网 发布:增值税发票数据导出 编辑:程序博客网 时间:2024/06/13 05:29

题目:

Given an integer, convert it to a roman numeral.

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

Subscribe to see which companies asked this question.

Show Tags

Show Similar Problems

解析思路:

在阿拉伯数字中我们求千位数,百位数,十位数,各位数的时候,一般是使用求商和求余的方式,这里我们也是一样
罗马数字规则可以参看另一篇博客点击打开链接

代码:

public String intToRoman(int num){String[] numerals = {"","I","II","III","IV","V","VI","VII","VIII","IX","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","M","MM","MMM"};String roman = "";roman = roman+numerals[30+num/1000]+numerals[20+num/100%10]+numerals[10+num/10%10]+numerals[num%10];return roman;}}