자료구조 및 알고리즘/알고리즘

[알고리즘] 이것이 취업을 위한 코딩 테스트다 - Chapter 3

benjykim 2021. 1. 19. 22:38
반응형

그리디

  1. 큰 수의 법칙
  • 소스코드

    n,m,k = map(int, input().split(" "))
    arr = list(map(int, input().split(" ")))
    
    arr.sort(reverse=True)
    
    first_value = arr[0]
    second_value = arr[1]
    
    q, r = divmod(m, k+1)
    sum = (first_value * k + second_value) * q + first_value * r
    print("sum = ", sum)
  1. 숫자 카드 게임
  • 소스코드

    n,m= map(int, input().split(" "))
    answer =  []
    
    for i in range(n):
        data = list(map(int, input().split(" ")))
        answer.append(min(data))
    
    print(max(answer))
  1. 1이 될 때까지
  • 소스코드

    n,k= map(int, input().split(" "))
    count = 0
    
    while n > 1:
        if n % k == 0:
            n //= k
        else:
            n -= 1
        count += 1
    
    print("count = ", count)
반응형