[アルゴリズム]プログラマ-予測対戦テーブル


プログラマ-予想対戦表

説明する

def solution(n,a,b):
    arr1 = [i for i in range(1, n+1)]
    result = []
    index = 0
    count = 1
    while True:
        while index < len(arr1):
            if arr1[index] == a and arr1[index+1] == b or arr1[index] == b and arr1[index+1] == a:
                return count
                exit()
            elif arr1[index] == a or arr1[index+1] == a:
                result.append(a)
                index += 2
            elif arr1[index] == b or arr1[index + 1] == b:
                result.append(b)
                index += 2
            else:
                result.append(arr1[index])
                index += 2

        arr1 = result[:]
        result = []
        index = 0
        count += 1

    return 0
これは直接シミュレーションで実現した.

他人を解く

def solution(n,a,b):
    count = 0
    while a != b:
        a = (a+1) // 2
        b = (b+1) // 2
        count += 1

    return count

print(solution(8, 4, 7), 3)

ソース:https://mungto.tistory.com/205