1부터 3999까지의 10진수를 로마숫자로 변환하기
정수가 주어지면 이를 동등한 로마 숫자 표현으로 변환합니다.
메모: 다음은 로마 기호 목록입니다(뺄셈 포함).
| 상징 | 값 |
|---|---|
| 나 | 1 |
| IV | 4 |
| 다섯 | 5 |
| 9 | 9 |
| 엑스 | 10 |
| 특대 | 40 |
| 엘 | 50 |
| XC | 90 |
| 기음 | 100 |
| CD | 400 |
| 디 | 500 |
| CM | 900 |
| 중 | 1000 |
예:
입력: 9
산출: 9
설명: 9는 빼기 표기법을 사용하여 로마 숫자로 'IX'로 작성됩니다. 즉, 큰 숫자 앞에 작은 숫자를 배치합니다.
I= 1X= 10IX10 - 1 =을 의미합니다. 9입력: 40
산출: 특대
설명: 40은 빼기 표기법을 사용하여 로마 숫자로 'XL'로 표시됩니다. 즉, 큰 숫자 앞에 작은 숫자를 배치합니다.
X= 10리터 = 50XL50 - 10 =을 의미합니다. 40
[범용 솔루션] - O(n) 시간과 O(n) 공간
주어진 숫자를 1000 900 500 400 100 90 50 40 10 9 5 4 순서로 기본 값과 비교합니다. 주어진 숫자보다 작은 가장 큰 기본 값을 찾으면 숫자를 기본 값으로 나누고 더 작은 기본 값과 몫에 대해 프로세스를 반복합니다. 구한 밑값에 해당하는 로마 기호를 몫과 같은 횟수만큼 결과에 더하고 나머지에 대해서도 이 과정을 반복합니다.
예제 3549를 통해 접근 방식을 이해해 보겠습니다.
반복 1
- 3549 이후 >= 1000 ; 가장 큰 기본 값은 처음에는 1000입니다.
- 3549/1000을 나눕니다. 몫 = 3 res = 'MMM'(참고 M은 1000에 속함)
- 나머지 = 549
반복 2
- 1000 > 549 >= 500 ; 가장 큰 기본 값은 500입니다.
- 549/500으로 나눕니다. 항상 = 1 .res = '300'
- 나머지 = 49
반복 3
- 50 > 49 >= 40 ; 가장 큰 기본 값은 40입니다.
- 49/40으로 나눕니다. 몫 = 1 res = 'MMMDXL'
- 나머지 = 9.
반복 4
- 목록에 9번이 있습니다. 입술 = 'MMMDXL'
- 나머지 = 0.
#include using namespace std ; // Function to convert decimal to Roman Numerals string toRoman ( int x ) { // array of values and symbols vector < int > base = { 1 4 5 9 10 40 50 90 100 400 500 900 1000 }; vector < string > sym = { 'I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M' }; // to store result string res = '' ; // Loop from the right side to find // the largest smaller base value int i = base . size () - 1 ; while ( x > 0 ) { int div = x / base [ i ]; while ( div ) { res += sym [ i ]; div -- ; } // Repeat the process for remainder x = x % base [ i ]; i -- ; } return res ; } int main () { int x = 3549 ; cout < < toRoman ( x ); return 0 ; }
Java // Function to convert decimal to Roman Numerals public class RomanConverter { public static String toRoman ( int x ) { // array of values and symbols int [] base = { 1 4 5 9 10 40 50 90 100 400 500 900 1000 }; String [] sym = { 'I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M' }; // to store result StringBuilder res = new StringBuilder (); // Loop from the right side to find // the largest smaller base value int i = base . length - 1 ; while ( x > 0 ) { int div = x / base [ i ] ; while ( div > 0 ) { res . append ( sym [ i ] ); div -- ; } // Repeat the process for remainder x = x % base [ i ] ; i -- ; } return res . toString (); } public static void main ( String [] args ) { int x = 3549 ; System . out . println ( toRoman ( x )); } }
Python # Function to convert decimal to Roman Numerals def to_roman ( x ): # array of values and symbols base = [ 1 4 5 9 10 40 50 90 100 400 500 900 1000 ] sym = [ 'I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M' ] # to store result res = '' # Loop from the right side to find # the largest smaller base value i = len ( base ) - 1 while x > 0 : div = x // base [ i ] while div : res += sym [ i ] div -= 1 # Repeat the process for remainder x %= base [ i ] i -= 1 return res x = 3549 print ( to_roman ( x ))
C# // Function to convert decimal to Roman Numerals public class RomanConverter { public static string ToRoman ( int x ) { // array of values and symbols int [] baseValues = { 1 4 5 9 10 40 50 90 100 400 500 900 1000 }; string [] symbols = { 'I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M' }; // to store result string res = '' ; // Loop from the right side to find // the largest smaller base value int i = baseValues . Length - 1 ; while ( x > 0 ) { int div = x / baseValues [ i ]; while ( div > 0 ) { res += symbols [ i ]; div -- ; } // Repeat the process for remainder x %= baseValues [ i ]; i -- ; } return res ; } public static void Main () { int x = 3549 ; Console . WriteLine ( ToRoman ( x )); } }
JavaScript // Function to convert decimal to Roman Numerals function toRoman ( x ) { // array of values and symbols const base = [ 1 4 5 9 10 40 50 90 100 400 500 900 1000 ]; const sym = [ 'I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M' ]; // to store result let res = '' ; // Loop from the right side to find // the largest smaller base value let i = base . length - 1 ; while ( x > 0 ) { let div = Math . floor ( x / base [ i ]); while ( div ) { res += sym [ i ]; div -- ; } // Repeat the process for remainder x %= base [ i ]; i -- ; } return res ; } let x = 3549 ; console . log ( toRoman ( x ));
산출
MMMDXLIX
시간 복잡도: O(n) 여기서 n은 변환을 저장하는 응답 문자열의 길이입니다.
보조 공간: 에)
[제한된 범위의 경우] - O(n) 시간 및 O(n) 공간
C++이 아이디어는 0에서 3999까지 변환할 수 있는 범위가 제한되어 있다는 사실에 기반을 두고 있습니다. 우리는 수천의 십만 자리와 일 자리에 해당하는 숫자를 분리한 다음 위치 값을 기반으로 각 숫자를 상응하는 로마 숫자로 매핑합니다.
- 다양한 몫에 대한 문자 M의 매핑을 저장합니다. 0 1 2 3
- 서로 다른 몫에 대한 CL과 I의 매핑을 0에서 9까지 저장합니다.
위의 매핑을 사용하여 결과 문자열을 직접 생성합니다.
#include using namespace std ; // Function to convert decimal to Roman Numerals string toRoman ( int val ) { // storing roman values of digits from 0-9 // when placed at different places vector < string > m = { '' 'M' 'MM' 'MMM' }; vector < string > c = { '' 'C' 'CC' 'CCC' 'CD' 'D' 'DC' 'DCC' 'DCCC' 'CM' }; vector < string > x = { '' 'X' 'XX' 'XXX' 'XL' 'L' 'LX' 'LXX' 'LXXX' 'XC' }; vector < string > i = { '' 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII' 'IX' }; // Converting to roman string thousands = m [ val / 1000 ]; string hundreds = c [( val % 1000 ) / 100 ]; string tens = x [( val % 100 ) / 10 ]; string ones = i [ val % 10 ]; string ans = thousands + hundreds + tens + ones ; return ans ; } int main () { int val = 3549 ; cout < < toRoman ( val ); return 0 ; }
Java import java.util.* ; public class GfG { // Function to convert decimal to Roman Numerals public static String toRoman ( int val ) { // storing roman values of digits from 0-9 // when placed at different places String [] m = { '' 'M' 'MM' 'MMM' }; String [] c = { '' 'C' 'CC' 'CCC' 'CD' 'D' 'DC' 'DCC' 'DCCC' 'CM' }; String [] x = { '' 'X' 'XX' 'XXX' 'XL' 'L' 'LX' 'LXX' 'LXXX' 'XC' }; String [] i = { '' 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII' 'IX' }; // Converting to roman String thousands = m [ val / 1000 ] ; String hundreds = c [ ( val % 1000 ) / 100 ] ; String tens = x [ ( val % 100 ) / 10 ] ; String ones = i [ val % 10 ] ; String ans = thousands + hundreds + tens + ones ; return ans ; } public static void main ( String [] args ) { int val = 3549 ; System . out . println ( toRoman ( val )); } }
Python # Function to convert decimal to Roman Numerals def toRoman ( val ): # storing roman values of digits from 0-9 # when placed at different places m = [ '' 'M' 'MM' 'MMM' ] c = [ '' 'C' 'CC' 'CCC' 'CD' 'D' 'DC' 'DCC' 'DCCC' 'CM' ] x = [ '' 'X' 'XX' 'XXX' 'XL' 'L' 'LX' 'LXX' 'LXXX' 'XC' ] i = [ '' 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII' 'IX' ] # Converting to roman thousands = m [ val // 1000 ] hundreds = c [( val % 1000 ) // 100 ] tens = x [( val % 100 ) // 10 ] ones = i [ val % 10 ] ans = thousands + hundreds + tens + ones return ans if __name__ == '__main__' : val = 3549 print ( toRoman ( val ))
C# using System ; public class GfG { // Function to convert decimal to Roman Numerals public static string toRoman ( int val ) { // storing roman values of digits from 0-9 // when placed at different places string [] m = { '' 'M' 'MM' 'MMM' }; string [] c = { '' 'C' 'CC' 'CCC' 'CD' 'D' 'DC' 'DCC' 'DCCC' 'CM' }; string [] x = { '' 'X' 'XX' 'XXX' 'XL' 'L' 'LX' 'LXX' 'LXXX' 'XC' }; string [] i = { '' 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII' 'IX' }; // Converting to roman string thousands = m [ val / 1000 ]; string hundreds = c [( val % 1000 ) / 100 ]; string tens = x [( val % 100 ) / 10 ]; string ones = i [ val % 10 ]; string ans = thousands + hundreds + tens + ones ; return ans ; } public static void Main ( string [] args ) { int val = 3549 ; Console . WriteLine ( toRoman ( val )); } }
JavaScript // Function to convert decimal to Roman Numerals function toRoman ( val ) { // storing roman values of digits from 0-9 // when placed at different places let m = [ '' 'M' 'MM' 'MMM' ]; let c = [ '' 'C' 'CC' 'CCC' 'CD' 'D' 'DC' 'DCC' 'DCCC' 'CM' ]; let x = [ '' 'X' 'XX' 'XXX' 'XL' 'L' 'LX' 'LXX' 'LXXX' 'XC' ]; let i = [ '' 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII' 'IX' ]; // Converting to roman let thousands = m [ Math . floor ( val / 1000 )]; let hundreds = c [ Math . floor (( val % 1000 ) / 100 )]; let tens = x [ Math . floor (( val % 100 ) / 10 )]; let ones = i [ val % 10 ]; let ans = thousands + hundreds + tens + ones ; return ans ; } let val = 3549 ; console . log ( toRoman ( val ));
산출
MMMDXLIX