๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€] 4573 ๋ฒˆ ์…€ํ”„๋„˜๋ฒ„

by rindev 2020. 11. 19.

www.acmicpc.net/problem/4673

 

4673๋ฒˆ: ์…€ํ”„ ๋„˜๋ฒ„

์…€ํ”„ ๋„˜๋ฒ„๋Š” 1949๋…„ ์ธ๋„ ์ˆ˜ํ•™์ž D.R. Kaprekar๊ฐ€ ์ด๋ฆ„ ๋ถ™์˜€๋‹ค. ์–‘์˜ ์ •์ˆ˜ n์— ๋Œ€ํ•ด์„œ d(n)์„ n๊ณผ n์˜ ๊ฐ ์ž๋ฆฌ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ํ•จ์ˆ˜๋ผ๊ณ  ์ •์˜ํ•˜์ž. ์˜ˆ๋ฅผ ๋“ค์–ด, d(75) = 75+7+5 = 87์ด๋‹ค. ์–‘์˜ ์ •์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ,

www.acmicpc.net

 

 

๋ฌธ์ œ๊ฐ€ ๋ญ”๋ง์ธ์ง€ ํ•˜์•„์•„์•ˆ์ฐธ ๋“ค์—ฌ๋‹ค๋ดค๋‹คใ…กใ…ก

๋ฐฑ์ค€์ด๋‚˜ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋Š” ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ •๋ฆฌํ•  ์ƒ๊ฐ์ด์—ˆ๋Š”๋ฐ ์ด๊ฒŒ.. ์ง„์งœ ๊นŒ๋จน๊ธฐ์ „์— ์จ๋‘ฌ์•ผํ• ๊ฑฐ๊ฐ™์•„์„œ ๊ธ‰ํ•˜๊ฒŒ ์ž‘์„ฑ์ค‘๐Ÿ˜ฃ

 

๋ถ„๋ช… ์ž๋ฐ”๋กœ๋„ ํ’€์—ˆ๋˜๊ฑด๋ฐ ์™œ์บ ์˜ค๋ž˜๊ฑธ๋ ธ๋Š”์ง€ ์•Œ์ˆ˜์—†์ง€๋งŒ ์•”ํŠผ ํ’€์ด๋Š” ์ด๋ ‡๋‹ค

 

1๋ถ€ํ„ฐ 10000์‚ฌ์ด์˜ ์…€ํ”„๋„˜๋ฒ„๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด for๋ฅผ 1๋ถ€ํ„ฐ 10000๊นŒ์ง€ ๋ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๋ฉด

i, hap, tmp (i๋Š” ์ธ๋ฑ์Šค, hap์€ ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ์ดํ•ฉ, tmp๋Š” i+hap)

1, 1, 2

2, 2, 4

3, 3, 6

4, 4, 8

..

10, 1, 11

11, 2, 13

...

์ด๋Ÿฐ์‹์œผ๋กœ ์ง„ํ–‰์ด ๋  ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ ํฌ๊ธฐ๊ฐ€ 10001 ์งœ๋ฆฌ์ธ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋†“๊ณ  (์™œ 10001์ด๋ƒ๋ฉด index๋ž‘ tmp๋ž‘ ๋˜‘๊ฐ™์ด ์“ฐ๊ณ ์‹ถ์–ด์„œ)

arr[tmp] = 1 ๋กœ ๋ณ€๊ฒฝ์„ ํ•ด์คŒ(์ด๋†ˆ๋“ค์€ ์…€ํ”„๋„˜๋ฒ„๊ฐ€ ์•„๋‹ˆ๋‹ค! ๋ผ๊ณ  ํ‘œ์‹œ)

arr[0]์„ ๋”ฐ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ•˜๋‚˜? ํ–ˆ๋Š”๋ฐ ๋ณด๋‹ˆ๊นŒ 

0, 0, 0 ์ด์–ด์„œ arr[0]=1 ์ด ๋ผ์žˆ์—ˆ๋‹ค... ๊ทธ๋ž˜์„œ ๋”ฐ๋กœ ํ•ด์ฃผ์ง€๋Š” ์•Š์•˜์Œ

๊ทธ๋Ÿฌ๋ฉด index๊ฐ€ ์…€ํ”„๋„˜๋ฒ„์ธ ๋†ˆ๋“ค์€ 0์ด ๋“ค์–ด์žˆ์„๊ฑฐ๊ณ , ์•„๋‹Œ ๋†ˆ๋“ค์€ 1์ด ๋“ค์–ด์žˆ์„๊ฑฐ๋‹ˆ๊นŒ

enumerate๋ฅผ ์‚ฌ์šฉํ•ด์„œ v๊ฐ€ 0์ธ ์• ๋“ค์˜ i๋ฅผ ์ถœ๋ ฅํ•ด์คŒ

 

# 4673 ๋ฒˆ : ์…€ํ”„๋„˜๋ฒ„
def def_4673():
  def get_num_hap(n):
    remain = 0
    while n > 0:
      remain += n % 10
      n = n//10
    return remain

  arr_num = [0]*10001

  for i in range(10001):
    hap = get_num_hap(i)    
    tmp = hap + i
    if tmp < 10001:
      arr_num[tmp] = 1

  for i, v in enumerate(arr_num):
    if v == 0:
      print(i)

 

 

์ค‘๊ฐ„์€ ์ž˜๋ผ๋จน๊ณ  ๊ฒฐ๊ณผ์ฐฝ์€ ์ด๋ ‡์Šต๋‹ˆ๋‹ค(โŒฃ_โŒฃ”)

๋Œ“๊ธ€