๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] n์ง„์ˆ˜ ๊ฒŒ์ž„ (Python)

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • ์ž…๋ ฅ
    • ์ง„๋ฒ• n, ๋ฏธ๋ฆฌ ๊ตฌํ•  ์ˆซ์ž์˜ ๊ฐฏ์ˆ˜ t, ๊ฒŒ์ž„์— ์ฐธ๊ฐ€ํ•˜๋Š” ์ธ์› m, ํŠœ๋ธŒ์˜ ์ˆœ์„œ p
  • ์ถœ๋ ฅ
    • ํŠœ๋ธŒ๊ฐ€ ๋งํ•ด์•ผ ํ•˜๋Š” ์ˆซ์ž t๊ฐœ๋ฅผ ๊ณต๋ฐฑ ์—†์ด ์ฐจ๋ก€๋Œ€๋กœ ๋‚˜ํƒ€๋‚ธ ๋ฌธ์ž์—ด. ๋‹จ, 1015๋Š” ๊ฐ๊ฐ ๋Œ€๋ฌธ์ž AF๋กœ ์ถœ๋ ฅ

2. ์ฝ”๋“œ

def convert(n, base):#n:10์ง„์ˆ˜ base: ์ง„์ˆ˜ k
    T = "0123456789ABCDEF"
    q, r = divmod(n, base)
    if q == 0:
        return T[r]
    else:
        return convert(q, base) + T[r]

def solution(n, t, m, p):#์ง„๋ฒ• n, ๋ฏธ๋ฆฌ ๊ตฌํ•  ์ˆซ์ž์˜ ๊ฐฏ์ˆ˜ t, ๊ฒŒ์ž„์— ์ฐธ๊ฐ€ํ•˜๋Š” ์ธ์› m, ํŠœ๋ธŒ์˜ ์ˆœ์„œ p
    answer = '' #์ •๋‹ต๋ฆฌ์ŠคํŠธ
    tube = ''
    for num in range(m*t):
        answer+=convert(num, n)

    for i in range(p-1, m*t,m):
        tube+= answer[i]

    return tube

3. ํšŒ๊ณ 

  • 10์ง„์ˆ˜๋ฅผ k์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
    • ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค
    • ๋ชซ์ด 0์ด ๋  ๋•Œ๊นŒ์ง€ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•ด์•ผํ•˜๋ฉฐ ์ด ๊ณผ์ •์—์„œ ๋‚˜์˜จ ๋‚˜๋จธ์ง€๋“ค์„ ์—ญ์ˆœ์œผ๋กœ ๊ฒฐํ•ฉํ•ด์•ผํ•œ๋‹ค.
  • ์ฐธ๊ฐ€์ธ์›*ํŠœ๋ธŒ๊ฐ€ ๋งํ•ด์•ผํ•˜๋Š” ์ˆซ์ž ๊ฐฏ์ˆ˜๋งŒํผ ์ˆซ์ž(answer)๋ฅผ ๊ตฌํ•˜๊ณ 
  • p-1(์ธ๋ฑ์Šค๋กœ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด) ๋ถ€ํ„ฐ m*t๊นŒ์ง€ m์”ฉ ๊ฑด๋„ˆ๋›ฐ๋ฉด์„œ ํŠœ๋ธŒ์˜ ๊ฐ’์„ ์กฐํšŒํ•˜์—ฌ tube์— ๋ถ™์—ฌ์คŒ

4. ์ฐธ๊ณ 

https://codingdojang.com/scode/458