BOJ/백준-1755-python

cosmos·2022년 3월 1일
0
post-thumbnail

문제

풀이

  • 정수 m, n이 주어지면 m이상 n이하의 정수를 숫자 하나씩 읽었을 때를 기준으로 사전순으로 출력하라.
  • 한 줄에 10개씩 출력하라.
    -> 0부터 9까지 딕셔너리로 알파벳을 입력하였다.
    -> 정수를 문자열로 변환후, 한 자리씩 분리하여 딕셔너리 키 값에 맞춰 벨류를 매칭시켰다.
    -> 딕셔너리 벨류값에의한 정렬은 lambda를 이용하여 구현하였다.
    -> 한 줄에 열개씩 출력은 for반복문 + 증감식 + 슬라이싱으로 구현하였다.

코드

# https://www.acmicpc.net/problem/1755
# boj, 1755: 숫자 놀이, python3
import sys

input = sys.stdin.readline

def solve(m, n):
    number_dict = {
        0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four',
        5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'
    }
    nums = [list(map(int, str(x))) for x in range(m, int(n)+1)]
    convert_dict = {}

    for x in nums:
        word = ""
        num = ""
        for y in x:
            word += number_dict[y]
            num += str(y)
        convert_dict[num] = word

    sort_dict = sorted(convert_dict.items(), key=lambda x: x[1])
    result = [int(x[0]) for x in sort_dict]
    tmp = 10

    for x in range(0, len(result), 10):
        print(*result[x:tmp])
        tmp += 10

if __name__ == '__main__':
    m, n = map(str, input().split())

    solve(int(m), n)

결과

출처 & 깃허브

BOJ 1755
github

0개의 댓글