"012-integer to Roman (number to Roman character)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
Given an integer, convert it to a Roman numeral.
Input is guaranteed to being within the range from 1 to 3999.
Main Topic
Enter a number, turn it into a Roman numeral, enter the number between [1, 3999].
Representation of Roman Numerals:
Single Digit example: (I, 1) (II, 2) (III, 3) (IV, 4) (V, 5) (VI, 6) (VII, 7) (VIII, 8) (IX, 9)
10-digit Example: (X, ten) (XI, one) (XII, +) (XIII, +) (XIV, XV) (XVII,) (XVIII, +) (XIX, X) (the ",") (XXI, 2 1) (XXII, +) (XXIX, XXXIV) (XXX, +) (XXXV, ET) (XXXIX, M) (XL, Max) (L, LI) (LV, p) (LX, 65 (LXXX, XCIII) (XC, XCV) (XCVIII, 98) (XCIX, 99)
Examples of the Hundred: (C, +) (CC, +) (CCC, +) (CD, D) (DC, Max) (DCC, DCCC) (CM, D) (CMXCIX, 999)
Number of thousand Examples: (M, +) (MC, 1100) (MCD, 1400) (MD, MDCLXVI) (MDC, 1666) (MDCCCLXXXVIII, 1888) (Mdcccxcix, 1899) (MCM , 1900) (MCMLXXVI, 1976) (MCMLXXXIV, 1984) (MCMXC, 1990) (MM, M) (Mmmcmxcix, 3999)
Thinking of solving problems
A two-dimensional array is created to hold 1-9 of the digit notation on each digit, and the number is calculated for the operator, and the character of each digit is computed from the low level, and the value to the final result.
Code Implementation
Public classSolution { PublicStringInttoroman(intnum) {string[][]Base=Newstring[][]{{"I","II","III","IV","V","VI","VII","VIII","IX"},//single-digit representation{"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},//10-bit representation{"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},//The expression of a hundredfold{"M","MM","MMM","","","","","",""}};//Thousands of representationsString result ="";one digit (from small to large) is shown after each addition //I record the number of digits currently being processed for(inti =0; Num! =0; Num/=Ten, i++) {//If it is not 0, indicating that there is a value on this digit, to add the Operation if(num%Ten!=0) {//Stitching resultsresult =BaseI [Num%Ten-1] + result; } }returnResult }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46963375"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "012-integer to Roman (Digital to Roman character)"