아이공의 AI 공부 도전기

[CodeUp] 1902번 (재귀함수) n부터 1까지 출력하기 (Python)

 

 

 

Python

 

방법 1 - 메모리 27724 / 시간 16 / 코드 길이 

def o(n):
  if n == 1:
    print(1)
  else: 
    print(n)    
    o(n-1)   

o(int(input()))

방법 2 - 메모리 27724 / 시간 16 / 코드 길이 

def o(n):
  if n<1:
    return
  print(n)
  o(n-1)  

o(int(input()))

방법 3 - 메모리 27724 / 시간 16 / 코드 길이 

def o(n):
  print(n)
  if n != 1:
    o(n-1)      
o(int(input()))

 

 

 

 

 

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading