아이공의 AI 공부 도전기

[Baekjoon] 1057번 : 토너먼트 (Python, 수학, 브루트포스)

 

     

 

 

https://www.acmicpc.net/problem/1057

 

1057번: 토너먼트

김지민은 N명이 참가하는 스타 토너먼트에 진출했다. 토너먼트는 다음과 같이 진행된다. 일단 N명의 참가자는 번호가 1번부터 N번까지 배정받는다. 그러고 난 후에 서로 인접한 번호끼리 스타를

www.acmicpc.net

 

코드 링크

https://github.com/stellaluminary/Baekjoon

 

GitHub - stellaluminary/Baekjoon

Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.

github.com

 

Python

 

방법 1 - 메모리 30840KB / 시간 80ms / 코드 길이 282B

 

n, j, h = map(int, input().split())
round = 1
while 1:
    if abs(j-h) == 1 and j//2 != h//2:
        print(round)
        break

    if j % 2 == 1:
        j = (j+1)//2
    else:
        j //= 2

    if h % 2 == 1:
        h = (h + 1) // 2
    else:
        h //= 2

    round += 1

 

방법 2 - 메모리 30840KB / 시간 76ms / 코드 길이 117B

 

n,start,end=map(int, input().split())
cnt=0
while start!=end:
  start -= start//2
  end -= end//2
  cnt+=1
print(cnt)

 

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading