๋ฐ์ํ
๐ป ๋ฐฑ์ค 2331๋ฒ [๋ฐ๋ณต์์ด]
ํ์ด)
์ฒซ๋ฒ์งธ ์ ์ถ : ๋ฐํ์์๋ฌ(ModuleNotFoundError) ๋ธ
์๊ณ ๋ณด๋ ๋ฐฑ์ค์์๋ numpy๋ฅผ ๋ชป ์ฐ๊ฒ ํจ...;;^^
# ์ฒซ๋ฒ์งธ
import numpy as np
a,p = map(int,input().split())
visited = {}
stack = []
def cal(a,p) :
if a in visited :
return stack.pop()
else :
visited[a] = 1
stack.append(a)
return cal(np.sum([int(i)**p for i in str(a)]),p)
dup_n = cal(a,p)
print(list(visited.keys()).index(np.sum([int(i)**p for i in str(dup_n)])))
# ==> ๋ฐํ์ ์๋ฌ(ModuleNotFoundError) : numpy์๋...;;
๋๋ฒ์งธ ์ ์ถ
# ์ต์ข
์ ์ถ
a,p = map(int,input().split())
visited = {}
stack = []
def cal(a,p) :
if a in visited :
return stack.pop()
else :
visited[a] = 1
stack.append(a)
return cal(sum([int(i)**p for i in str(a)]),p)
dup_n = cal(a,p)
print(list(visited.keys()).index(sum([int(i)**p for i in str(dup_n)])))
# ==> ๋ฉ๋ชจ๋ฆฌ : 28776K4B, ์๊ฐ : 64ms, ์ฝ๋๊ธธ์ด : 323B
dfs๋ฅผ ๊ณต๋ถํ ๋ ๋๋๋ก์ด๋ฉด ์ฌ๊ทํจ์๋ฅผ ์ฌ์ฉํด๋ณด๋ ค๊ณ ๋ ธ๋ ฅํจ
์ ์ฝ๋์์๋ visited๋ฅผ dictionary๋ก ์ฌ์ฉํ์๊ณ , stack๊ณผ ์ฌ๊ทํจ์ cal์ ์ฌ์ฉํ์๋ค
728x90
๋ฐ์ํ
'๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค > DFS & BFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] DFS/BFS level2 ํ๊ฒ๋๋ฒ (0) | 2022.03.24 |
---|---|
[๋ฐฑ์ค 1012] [dfs/bfs] ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2021.04.29 |
[๋ฐฑ์ค 1012] [dfs/bfs] ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2021.04.24 |
[๋ฐฑ์ค 2667] [dfs/bfs] ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (0) | 2021.04.23 |
[๋ฐฑ์ค 2178] [dfs/bfs] ๋ฏธ๋กํ์ (0) | 2021.04.23 |
[๋ฐฑ์ค 1260] [dfs/bfs] BFS์ DFS โ (0) | 2021.04.23 |
[๋ฐฑ์ค 2606] [dfs/bfs] ๋ฐ์ด๋ฌ์ค (0) | 2021.04.22 |