Preface this weekend, plus the five days of this week, I need to help my mentor to build a project that is ending with the bad news of beiyou. It is very hard, but the kindness of Zhiyu is indeed too great. I can only do my best.
However, the project is controlled by the svn version library, and the submission record of github is blank for another day. It seems quite uncomfortable. If I don't submit code on github one day, I must submit it on svn.
Question Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Thinking of the title is not difficult, is less than 3999 of Arabic numerals into the Roman numerals, there was an article written before the characteristics of the Roman numerals, see the link: http://blog.csdn.net/wzy_1988/article/details/17057929
Taking 3849 as an example, we only need to consider the number of each digit. For each digit, digit is divided into the following situations: digit = 00 <digit <= 3 digit = 45 <= digit <= 9 digit = 9 for each digit, You can process these five conditions separately.
AC code
public class Solution { public String intToRoman(int num) { StringBuilder result = new StringBuilder(); char[] roman = {'I', 'V', 'X', 'L', 'C', 'D', 'M'}; int digit, base = 1000; for (int i = roman.length + 1; i >= 0 && num > 0; i -= 2, base /= 10) { digit = num / base; if (digit == 0) { continue; } else if (digit <= 3) { for (int j = 0; j < digit; j ++) { result.append(roman[i - 2]); } } else if (digit == 4) { result.append(roman[i - 2]); result.append(roman[i - 1]); } else if (digit <= 8) { result.append(roman[i- 1]); for (int j = digit - 5; j > 0; j --) { result.append(roman[i - 2]); } } else if (digit == 9) { result.append(roman[i - 2]); result.append(roman[i]); } num = num % base; } return result.toString(); }}