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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
K = int(input())

# order๋ฒˆ์งธ ์ˆ˜์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค
def get_digit(order):
    # assert order >= 1
    n = 0
    k = 1
    while n <= order:
        n += 2 ** k
        k += 1
    return k - 1

def get_number(order):
    digit = get_digit(order)
    # ์ด ์ž๋ฆฟ์ˆ˜์—์„œ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ค‘ ๋ช‡๋ฒˆ์งธ ์œ„์น˜์ธ์ง€ ๊ตฌํ•จ
    count = order
    n = 1
    while n < digit:
        count -= 2 ** n
        n += 1
    
    answer = []
    n = 1
    while n <= digit:
        # ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋ฉด ์ด ์ž๋ฆฌ๋Š” 4
        # ๋‚˜๋จธ์ง€๊ฐ€ 1์ด๋ฉด ์ด ์ž๋ฆฌ๋Š” 7
        rem = count % 2
        if rem:
            answer.append("7")
        else:
            answer.append("4")
        
        count = count // 2
        # print(answer)
        n += 1
    
    answer.reverse()
    return ''.join(answer)

print(get_number(K - 1))


3. ํ•ด์„ค

  • ํ•ด์„ค

    k ๋ฒˆ์งธ ์ˆ˜์˜ ์ž๋ฆฟ์ˆ˜ ๊ฐฏ์ˆ˜ digit๋ฅผ ๊ตฌํ•œ๋‹ค

    k ๋ฒˆ์งธ ์ˆ˜ ์ž๋ฆฟ์ˆ˜์—์„œ ๋‚˜ํƒ€๋‚˜๋Š” ํŒจํ„ด์€ 2^digit ๊ฐœ ์ž„์„ ์ด์šฉํ•œ๋‹ค

    n ๋ฒˆ์งธ ์ž๋ฆฟ์ˆ˜์˜ ์ˆซ์ž๋Š” 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋ฉด 4, 1์ด๋ฉด 7 ๋กœ ์ง€์ •

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