Post

๐Ÿน 4์™€ 7

1. ๋ฌธ์ œ ๋งํฌ

2877๋ฒˆ: 4์™€ 7


2. ์ฝ”๋“œ

Python3 31120kb 40ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
count = [2**i for i in range(31)]
K = int(input())

num = 0
for i in range(1, 31):
    num += count[i]
    if K <= num:
        num_len = i  # K๋ฒˆ์งธ ์ˆซ์ž์˜ ๊ธธ์ด
        break

k_location = K - (num - count[num_len])
result = ''

div_num = 2
while num_len:
    n = k_location % div_num
    if n == 0 or n > (div_num // 2):
        result = '7' + result
    else:
        result = '4' + result
    num_len -= 1
    div_num *= 2

print(int(result))


3. ํ•ด์„ค

๋จผ์ € K๋ฒˆ์งธ ์ˆซ์ž์˜ ๊ธธ์ด๋ฅผ ํŒŒ์•…ํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” num_len

๊ฐ€์žฅ ์ž‘์€ ์ž๋ฆฌ์ˆ˜ ๋ถ€ํ„ฐ ๊ฐ’์„ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค. ๊ฐ ์ž๋ฆฌ์—๋Š” 4์™€ 7๋งŒ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 2์˜ num_len ์ œ๊ณฑ๋ฐฐ ์ž…๋‹ˆ๋‹ค.

ํ™•์ธํ•˜๋Š” ์ž๋ฆฌ๊ฐ€ ํ•œ ์นธ ์•ž์œผ๋กœ ๊ฐˆ ๋•Œ๋งˆ๋‹ค div_num(๋‚˜๋ˆ„์–ด์ฃผ๋Š” ์ˆ˜)์— 2๋ฅผ ๊ณฑํ•ด์ค๋‹ˆ๋‹ค. ์ด๋•Œ div_num์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ div_num์˜ 2๋ถ„์˜ 1๋ฐฐ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด โ€˜4โ€™, ํฌ๋‹ค๋ฉด(ํ˜น์€ 0์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง„๋‹ค๋ฉด) โ€˜7โ€™์„ ๊ฐ’์— ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

K๋ฒˆ์งธ ์ˆซ์ž์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•ด์ฃผ๊ณ  ๋ฌธ์ž์—ด๋กœ ๋‹ค๋ค˜๋˜ ๊ฐ’์„ intํ˜•์œผ๋กœ ํ˜•๋ณ€ํ™˜ ํ•ด์ค๋‹ˆ๋‹ค.

This post is licensed under CC BY 4.0 by the author.