728x90
4922번: Walk Like an Egyptian
Walk Like an Egyptian is an old multi-player board game played by children of the Sahara nomad tribes. Back in the old days, children would collect stones, and number each one of them. A game with N players requires N2 stones. Each player chooses N stones.
www.acmicpc.net
문제 풀이
수열의 규칙을 찾기 쉬운 문제였다. n번째 수는 모두 대각선에 위치해 있다.
[1, 3, 7, 13] 순으로 수열이 진행됨을 확인할 수 있다.
이 수열은 n이 커질수록 차가 2씩 증가하고 있다.
- 1과 3의 차 2
- 3과 7의 차 4
- 7과 13의 차 6
- ...
이를 코드로 작성해 풀 수 있었다.
정답 코드
lst = [0] * 1001
lst[1] = 1
lst[2] = 3
for i in range(3, 1000) : lst[i] = lst[i - 1] + lst[i - 1] - lst[i - 2] + 2
while True :
n = int(input())
if n == 0 : break
print("{:d} => {:d}".format(n, lst[n]))
728x90
'Algorithm > PS' 카테고리의 다른 글
[BOJ/백준] 5073 - 삼각형과 세 변 [python] (0) | 2023.03.12 |
---|---|
[BOJ/백준] 5013 - Death Knight Hero [python] (0) | 2023.03.12 |
[BOJ/백준] 4909 - Judging Olympia [python] (0) | 2023.03.12 |
[BOJ/백준] 4892 - 숫자 맞추기 게임 [python] (0) | 2023.03.12 |
[BOJ/백준] 4880 - 다음수 [python] (0) | 2023.03.12 |