반응형
문제) https://www.codingame.com/training/easy/the-descent
이 퍼즐로 배울 수 있는 것 : 반복문의 사용법
문제) Destiny ship 이 행성의 산과 부딪히지 않도록 한 턴에 가장 높은 산의 위치를 찾아서 파괴해서 무사히 착륙할 수 있도록 도와 주는 문제
문제해결)
solve it 버튼을 클릭하여 문제를 해결하여 봅니다.
1단계) 다음과 같이 왼쪽에 문제가 주어지고 오른쪽에 코딩을 통해 문제를 해결합니다.
더보기
import sys
import math
# The while loop represents the game.
# Each iteration represents a turn of the game
# where you are given inputs (the heights of the mountains)
# and where you have to print an output (the index of the mountain to fire on)
# The inputs you are given are automatically updated according to your last actions.
# game loop
while True:
max_h=0
max_index=-1
for i in range(8):
mountain_h = int(input()) # represents the height of one mountain.
if mountain_h > max_h:
max_h = mountain_h
max_index=i
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
# The index of the mountain to fire on.
print(max_index)
가장 최고의 높이를 찾는 max_h = 0 을 만들고 들어 오는 수 중에서 max_h 보다 큰 값이 있다면 max_h는 가장 큰 값으로 갱신하고 현재 들어온 순서를 max_index 에 기록 합니다.
입력이 다 들어 왔다면 가장 큰 위치의 순서를 출력하여 해당 산을 파괴 합니다.
2단계) PLAY ALL TESTCASE 를 클릭하여 문제가 해결 되었는지 테스트 해 봅니다.
3단계) SUBMIT 버튼을 클릭하여 코드를 제출하여 SCORE를 확인합니다.
THE DESCENT 퍼즐을 이용해서 Python 에서 8번 반복하는 for i in range(8) 의 구문을 이해 할 수 있습니다.
또한 8개의 산에서 가장 높은 산의 위치를 찾는 방법에 대해 이해 할 수 있습니다.
반응형
'정보 > 게임으로 배우는 알고리즘' 카테고리의 다른 글
[게임으로 배우는 알고리즘]MARS LANDER - EPISODE 1 (0) | 2023.03.02 |
---|---|
[게임으로 배우는 알고리즘]POWER OF THOR - EPISODE 1 (0) | 2023.03.02 |
[게임으로 배우는 알고리즘]ONBOARDING (0) | 2023.03.02 |