Skip to content

Instantly share code, notes, and snippets.

@Park-Developer
Created May 6, 2021 16:51
Show Gist options
  • Save Park-Developer/ca2469ecd683756299b581258a51cbfa to your computer and use it in GitHub Desktop.
Save Park-Developer/ca2469ecd683756299b581258a51cbfa to your computer and use it in GitHub Desktop.
Ring Buffer
# 원하는 개수만큼 값을 입력받아 마지막 n개를 저장
n = int(input('정수를 몇개 저장할까요?'))
a= [None]*n # 입력받은 값을 저장하는 배열
cnt = 0 # 정수를 입력받은 개수
while True:
a[cnt%n]=int(input((f'{cnt+1}번째정수를 입력하세요')))
cnt+=1
retry= input(f'계속할까요?()')
if retry in {'N', 'n'}: # N이나 n을 입력하면 더 이상 값을 받지 않음
break
i = cnt-n
if i<0 : i=0
while i< cnt:
print(f'{i+1}번째={a[i%n]}')
i+=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment