백준 20546번 기적의 매매법 풀이(Python, 구현, Silver5)

전승재·2024년 2월 17일
0

알고리즘

목록 보기
75/88

백준 기적의 매매법 문제 바로가기

문제 접근

준현이는 구매를 할 수는 있지만 매도를 하지는 않는다.
성민이는 3일 내내 가격이 하락하면 전량 매수하고 3일 내내 가격이 상승하면 전량 매도한다.
따라서 준현이는 주식을 구매할 수 있을 때마다 전량 매수한다.
성민이는 몇일동안 주식이 올랐는지 내렸는지에 따라 구매하기 때문에 이에 대한 카운트를 세어주어야 한다. 따라서 stack이라는 카운터를 넣어서 구해주었다.

문제 풀이

14일 진행

14일의 주가를 prices에 넣고 이를 for문으로 돌려서 하루하루 구매와 판매를 확인한다.

for i in prices:
    start_jun = junhyeon(start_jun, i)
    start_sung, stack = sungmin(start_sung, yesterday_price, i, stack)
    yesterday_price = i

money_jun = start_jun + yesterday_price*purchase_jun
money_sung = start_sung + yesterday_price*purchase_sung 
print(money_jun, money_sung)
if money_jun>money_sung:
    print("BNP")
elif money_jun<money_sung:
    print("TIMING")
elif money_jun==money_sung:
    print("SAMESAME")

준현이 매매법

준현이는 구매할 수만 있다면 모두 사기 때문에 아래와 같이 구매한다.

def junhyeon(money, price):
    global purchase_jun
    if money>=price:
        purchase = money//price
        money -= purchase*price
        purchase_jun += purchase
    return money # 남은 돈

성민이 매매법

성민이는 stack의 개수에 따라서 전량 매수할지 매도할지를 확인한다. 또한 성민이 함수에 stack을 더하고 빼는 로직을 넣어서 stack을 구한다.

def sungmin(money, yesterday_price, price, stack):
    global purchase_sung
    if stack>=0 and price-yesterday_price>0:
        stack += 1
    if stack<=0 and price-yesterday_price<0:
        stack-=1
    if stack<=0 and price-yesterday_price>0:
        stack = 1
    if stack>=0 and price-yesterday_price<0:
        stack = -1

    if stack>=3:
        money += purchase_sung * price
        purchase_sung = 0
        # 전량 매도
    elif stack<=-3:
        purchase = money//price
        money -= purchase*price
        purchase_sung += purchase
        # 전량 매수
    return money, stack

제출 코드

import sys
start = int(sys.stdin.readline())
prices = list(map(int, sys.stdin.readline().split()))

def junhyeon(money, price):
    global purchase_jun
    if money>=price:
        purchase = money//price
        money -= purchase*price
        purchase_jun += purchase
    return money # 남은 돈

def sungmin(money, yesterday_price, price, stack):
    global purchase_sung
    if stack>=0 and price-yesterday_price>0:
        stack += 1
    if stack<=0 and price-yesterday_price<0:
        stack-=1
    if stack<=0 and price-yesterday_price>0:
        stack = 1
    if stack>=0 and price-yesterday_price<0:
        stack = -1

    if stack>=3:
        money += purchase_sung * price
        purchase_sung = 0
        # 전량 매도
    elif stack<=-3:
        purchase = money//price
        money -= purchase*price
        purchase_sung += purchase
        # 전량 매수
    return money, stack

start_jun = start
start_sung = start
purchase_jun = 0
purchase_sung = 0
stack = 0
yesterday_price = prices[0]
for i in prices:
    start_jun = junhyeon(start_jun, i)
    start_sung, stack = sungmin(start_sung, yesterday_price, i, stack)
    yesterday_price = i

money_jun = start_jun + yesterday_price*purchase_jun
money_sung = start_sung + yesterday_price*purchase_sung 
if money_jun>money_sung:
    print("BNP")
elif money_jun<money_sung:
    print("TIMING")
elif money_jun==money_sung:
    print("SAMESAME")

0개의 댓글