[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ5] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 4673 - ์…€ํ”„๋„˜๋ฒ„

keyneneยท2022๋…„ 10์›” 8์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
1/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ5] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 4673 - ์…€ํ”„๋„˜๋ฒ„

๐Ÿ“œ๋ฌธ์ œ



๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

1~10000๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๋ฉด์„œ d(n)ํ•จ์ˆ˜๋กœ ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง„ ์…€ํ”„๋„˜๋ฒ„๊ฐ€ ์•„๋‹Œ ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ง‘ํ•ฉ์— ๋„ฃ๊ณ ,
๋‹ค์‹œ 1~10000๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๋ฉด์„œ ์…€ํ”„๋„˜๋ฒ„๊ฐ€ ์•„๋‹Œ ์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ์— ์—†๋Š” ์ˆ˜๋งŒ ์ถœ๋ ฅํ•ด๋ณด์ž
(๊ทธ ์ง‘ํ•ฉ์— ์—†์–ด์•ผ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” "์…€ํ”„๋„˜๋ฒ„"๋‹ˆ๊นŒ)

๐Ÿ‘‰๐Ÿป[๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜] : ๋ฌธ์ œ์—์„œ ์ œ์‹œ๋œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ฒ€์ƒ‰/ํŒŒ์•…ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

<<<์•„๋ž˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค (map/strํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•์„ ์ฐธ๊ณ )>>>
https://kbwplace.tistory.com/69


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. d(n)ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ : ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง„ ์…€ํ”„๋„˜๋ฒ„๊ฐ€ ์•„๋‹Œ ์ˆ˜ (nonSelfNum)๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ
  2. nonSelfNum *์ง‘ํ•ฉ(set()) ๋งŒ๋“ค๊ธฐ
  3. 1~10000๊นŒ์ง€ i ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋ฉด์„œ d(i)๋ฅผ ํ˜ธ์ถœํ•ด nonSelfNum ์ง‘ํ•ฉ์— ์ƒ์„ฑ์ž๋“ค์„ ์ €์žฅ
  4. ๋‹ค์‹œ 1~10000๊นŒ์ง€ j ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋ฉด์„œ j๊ฐ€ nonSelfNum์— ํฌํ•จ๋˜์–ด์ž‡๋Š”์ง€ ํ™•์ธํ•˜๋ฉด์„œ,
    ๋ฏธํฌํ•จ๋˜์–ด์žˆ๋Š” j๋งŒ์„ ์ถœ๋ ฅ (nonSelfNum์— ๋ฏธํฌํ•จ == SelfNum, ์…€ํ”„๋„˜๋ฒ„)
โ€ป ์ง‘ํ•ฉ(set())์€ ์ €์žฅ๋˜๋Š” ๊ฐ’๋“ค์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์ฃผ๋ฉฐ, ๊ต์ง‘ํ•ฉ(&), ํ•ฉ์ง‘ํ•ฉ(|),์ฐจ์ง‘ํ•ฉ(-) ๋“ฑ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•จ
   ์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•  ๋•Œ, ์ค‘๋ณต์ œ๊ฑฐ์šฉ์œผ๋กœ ์ž์ฃผ ์“ฐ์ด๊ณ ,
   ๋น„๊ตํ•  ๊ฐ’์ด ํ•ด๋‹น ์ง‘ํ•ฉ์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์šฉ๋„๋กœ๋„ ์ž์ฃผ ์“ฐ์ž„

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

#์ƒ์„ฑ์ž ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜ d(n)
def d(n):
    n = n + sum(map(int,str(n)))
    return n

#์…€ํ”„๋„˜๋ฒ„๊ฐ€ ์•„๋‹Œ ์ˆ˜๋“ค(์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋Š” ์ˆ˜๋“ค)์ด ๋“ค์–ด๊ฐˆ ์ง‘ํ•ฉ
nonSelfNum = set();

#nonSelfNum ์ง‘ํ•ฉ์— ๋“ค์–ด๊ฐˆ ์ˆ˜๋“ค ๋„ฃ๊ธฐ (1~10000)
for i in range(1,10001):
    nonSelfNum.add(d(i)) #1 ๋ถ€ํ„ฐ 10000๊นŒ์ง€ dํ•จ์ˆ˜์— ๋“ค์–ด๊ฐ„ ์ƒ์„ฑ์ž๋“ค ๋„ฃ์Œ (set์ด๋ฏ€๋กœ ์ค‘๋ณต์ œ๊ฑฐ๋จ)

#์…€ํ”„๋„˜๋ฒ„ ์ถœ๋ ฅํ•˜๊ธฐ
for j in range(1,10001):
    if j not in nonSelfNum:  
    #1๋ถ€ํ„ฐ 10000๊นŒ์ง€ ์ˆซ์ž ์ค‘ nonSelfNum์— ์ €์žฅ๋œ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๋Š” ์ˆ˜๋“ค์„ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅํ•จ
        print(j)

โœ๏ธ1. d(n)ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ

#str()์œผ๋กœ n์„ ๋ฌธ์ž์—ด(str)๋กœ ์ „ํ™˜ํ•˜๊ณ , 
#map()์œผ๋กœ ๊ฐ์ฒด๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ •์ˆ˜ํ˜•(int)์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ,
#sum()์œผ๋กœ ๋”ํ•ด์คŒ

def d(n):
    n = n + sum(map(int,str(n)))
    return n
  ex) d(123)์„ ํ•œ ๊ฒฐ๊ณผ
  ๐Ÿ‘‰๐Ÿป str(123)์„ ํ†ตํ•ด '123'์ด ๋˜๊ณ , map์œผ๋กœ intํ˜•์œผ๋กœ ๊ฐ์ฒดํ™”ํ•˜์—ฌ ์ €์žฅ, sum์œผ๋กœ ๊ฐ ์ž๋ฆฟ์ˆ˜ ๋”ํ•ด์คŒ

โœ๏ธ2. nonSelfNum ์ €์žฅํ•  ์ง‘ํ•ฉ ๋งŒ๋“ค๊ธฐ (set)

nonSelfNum = set();
#set()๋ฅผ ์ด์šฉํ•˜๋ฉด ๋น„์–ด์žˆ๋Š” ์ง‘ํ•ฉ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ (์ง‘ํ•ฉ์ •์˜)

โœ๏ธ3. 1~10000๊นŒ์ง€ i๋กœ d(i)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ nonSelfNum์„ ์ง‘ํ•ฉ์— ์ €์žฅ

#๋งŒ๋“ค์–ด๋‘” ์ง‘ํ•ฉ(nonSelfNum)์— ์ƒ์„ฑ์ž ๋„ฃ๊ธฐ
for i in range(1,10001):
    nonSelfNum.add(d(i)) 
    #1 ๋ถ€ํ„ฐ 10000๊นŒ์ง€ dํ•จ์ˆ˜์— ๋“ค์–ด๊ฐ„ ์ƒ์„ฑ์ž๋“ค ๋„ฃ์Œ (set์ด๋ฏ€๋กœ ์ค‘๋ณต์ œ๊ฑฐ๋จ)
  ์ง‘ํ•ฉ(set)์— ๊ฐ’์„ ๋„ฃ์„ ๋•Œ .add()ํ•จ์ˆ˜ ์‚ฌ์šฉ
  ์ง‘ํ•ฉ(set)์— ๋ณด์œ /์ถ”๊ฐ€๋œ ๊ฐ’์€ ์ž๋™์ ์œผ๋กœ ์ค‘๋ณต์ œ๊ฑฐ๋จ

โœ๏ธ4. 1~10000๊นŒ์ง€ j๋กœ nonSelfNum์— ํฌํ•จ์—ฌ๋ถ€ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋ฏธํฌํ•จ์‹œ ์ถœ๋ ฅ

for j in range(1,10001):
    if j not in nonSelfNum: 
        print(j)
  in set / not in set ์œผ๋กœ ์ง‘ํ•ฉ์— ํ•ด๋‹น ๊ฐ’์˜ ํฌํ•จ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
  ์œ„ ์˜ˆ์ œ์—์„œ๋Š” nonSelfNum์— ํฌํ•จ๋˜์ง€ ์•Š์€ ์ˆ˜๊ฐ€ SelfNum ์ด๋ฏ€๋กœ,
  j not in nonSelfNum๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฏธํฌํ•จ๋œ j๊ฐ’๋งŒ์„ ์ถœ๋ ฅํ•˜์˜€์Œ (์—ฌ์ง‘ํ•ฉ๊ฒ€์ƒ‰)

๐Ÿ“š๊ธฐ์กด ๋‚ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์˜ค๋ฅ˜์™€ ๋น„ํšจ์œจ์ ์ธ ์ด์œ  ์ •๋ฆฌ

โ€ป ๋‚ด๊ฐ€ ์ฒ˜์Œ ๊ตฌํ˜„ํ•œ d(n) ํ•จ์ˆ˜

์ž๋ฆฟ์ˆ˜๋Œ€๋กœ ์ผ€์ด์Šค๋ฅผ ๋‚˜๋ˆ ์„œ ๊ฐ์ž๋ฆฟ์ˆ˜์™€ n์„ ๋”ํ•ด์คŒ

def d(n):
    m = 0;
    if n < 10 :
        m = n+n;
    elif n < 100 :
        m = n + n//10 + (n-(n//10*10));
    elif n < 1000 :
        m = n + n//100 + ((n-n//100*100)//10) + (n-(n//10*10));
    elif n < 10000 :
        m = n + (n//1000) + ((n-n//1000*1000)//100) + ((n-(n//100*100))//10) + (n-(n//10*10));
    return m
โŒ ์ž๋ฆฟ์ˆ˜๋งˆ๋‹ค ์ผ€์ด์Šค๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง๐Ÿคฆ๐Ÿปโ€โ™€๏ธ
    ๋ถ„๋ช… ์œ„ ์ฝ”๋“œ ๊ธธ์ด๋ฅผ ์ค„์ผ๋งŒํ•œ ๋‚ด์žฅํ•จ์ˆ˜๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ํŒ๋‹จํ•จ
    ๋‹ค๋ฅธ ์œ ์ €/๋ธ”๋กœ๊ฑฐ๋“ค์˜ ๊ฒ€์ƒ‰/์—ด๋žŒํ•ด๋ณธ ๊ฒฐ๊ณผ, 
    "map()"๊ณผ "str()"๋กœ๋„ ์ถฉ๋ถ„ํžˆ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋จ

  1. ๋ฌธ์ œ์— ์ œ์‹œ๋œ ๋ชจ๋“  ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผํ•˜๋ฉด ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉ
    (๊ฐ€์žฅ ์ •ํ™•ํ•˜๋ฉฐ ๋ฌด์‹ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋Œ€์‹  ์ƒ๋Œ€์ ์œผ๋กœ ๊ตฌํ˜„์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆผ)
  2. ์ค‘๋ณต์ œ๊ฑฐ/ํฌํ•จ์—ฌ๋ถ€ ๋“ฑ์˜ ์ผ€์ด์Šค๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ set()์„ ์ด์šฉํ•˜์ž
    (๊ต์ง‘ํ•ฉ/์—ฌ์ง‘ํ•ฉ/ํ•ฉ์ง‘ํ•ฉ/์ฐจ์ง‘ํ•ฉ ๋“ฑ์€ ๋ค)
  3. ์ž๋ฆฟ์ˆ˜๋ฅผ ๋ถ„๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ str()๊ณผ map()์„ ์ด์šฉํ•˜์ž
    (๋ฐฑ์ค€์—์„œ ๋ช‡ ๋ฒˆ ๋‚˜์™”๋Š”๋ฐ ์ด๊ฑด ํ•  ๋•Œ๋งˆ๋‹ค ์ž๋ฆฟ์ˆ˜๋งˆ๋‹ค ์ผ€์ด์Šค ๋‚˜๋ˆ„๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•˜๊ณ ์žˆ์Œ๐Ÿคฆ๐Ÿปโ€โ™€๏ธ
    ๋ฐœ์ „์ด ์—†์œผ๋ฉด ์™ธ์šฐ๋„๋ก ํ•˜์ž)
  4. ์ฝ”๋“œ ๊ธธ๊ฒŒ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ์Ž„ํ•˜๋‹ค๋ฉด ๊ณ ์ง‘๋ถ€๋ฆฌ์ง€ ๋ง๊ณ  ๊ฒ€์ƒ‰์„ ํ•˜์ž....
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€