본문 바로가기
알고리즘

BOJ 16922번(로마 숫자 만들기)

by eunyoung 2023. 3. 27.

난이도


실버 III

 

 

풀이


풀이1

combinations_with_replacement를 이용해서 중복조합의 경우의 수를 구해서 그 해당 경우의 수에 해당하는 숫자를 구한후 중복하는 수는 set자료구조를 이용해서 제거해주면 되는 문제였다.

from itertools import combinations_with_replacement
import sys

input = sys.stdin.readline

N = int(input())
result=[]

for rommaNum in combinations_with_replacement(['I', 'V', 'X', 'L'], N):
    rommaNum=list(rommaNum)
    score=0
    for romma in rommaNum:
        if romma=='I':
            score+=1
        elif romma=='V':
            score+=5
        elif romma=='X':
            score+=10
        else:
            score+=50
    result.append(score)


result=set(result)

print(len(result))

 

풀이2

import sys
from itertools import combinations_with_replacement

input=sys.stdin.readline

N = int(input())
result=[]
numbers=[1,5,10,50]

for temp in combinations_with_replacement(range(4),N):
    sum=0
    for i in temp:
        sum+=numbers[i]
    result.append(sum)

print(len(set(result)))

 

 

 

 

문제 링크


https://www.acmicpc.net/problem/16922

 

16922번: 로마 숫자 만들기

2, 6, 10, 11, 15, 20, 51, 55, 60, 100을 만들 수 있다.

www.acmicpc.net

 

'알고리즘' 카테고리의 다른 글

BOJ 2580번(스도쿠)  (0) 2023.03.30
BOJ 18430번(무기 공학)  (0) 2023.03.28
BOJ 11054번 (가장 긴 바이토닉 부분 수열)  (0) 2023.03.26
BOJ 1038번(감소하는 수)  (0) 2023.03.26
BOJ 9324번(진짜 메세지)  (0) 2023.03.24