이미지


코드 입력

    fc, vc, p = map(int, input().split())
    
    def get_BEP(fc=fc, vc=vc, p=p):
        if p-vc == 0:
            return -1
        q = fc / (p-vc)
        if q > 0:
            return int(q) + 1
        else:
            return -1
    
    print(get_BEP())
 
손익 분기점 문제가 나와서 쉬울 줄 알았지만, 이상한데서 헤메고 말았다.
 
나름 금융과라고, 금방 뚝딱 풀줄 알았는데 그 놈의 시간초과 때문에 다른 로직을 생각해야했다.
 
처음에는 앞서 푼 문제처럼 if 문으로 예외처리하면서 하나씩 맞는지 테스트하는 로직이었는데,
 
시간초과 오류가 계속 발생했다.
 
그래서 한 번에 답을 알 수 있지 않을까? 라고 생각을 해서, 과거에 회계나 경제 과목 들을 때 썼던 식들을 다시 재조립하기 시작했다. 
 
bep 계산하는 식을 간단하게 작성하고, 우리가 구해야 하는 건 q (quantity)이기 때문에 식을 q에 대한 식으로 변경했다.
 
그리고, ZeroDivisionError를 피하기 위해 분모가 0일 경우에도 -1을 도출하도록 식을 만들었다.
 
이랬더니 숫자가 크든 작든, 바로 답을 도출해내고 효과적으로 시간을 줄일 수 있었다.
 
수학 좋아했으면서, 왜 어렵게 생각했는지 모르겠다..
 
    # 처음 고안한 로직
    
    fixed_cost, variable_cost, price_per_goods = map(int, input().split())
    selling_cnt = 1
    
    def break_even_point(fixed_cost=fixed_cost, variable_cost=variable_cost, 
                        selling_cnt=selling_cnt, price_per_goods=price_per_goods):
    
        before_diff = 0
        while True:
            total_revenue = selling_cnt * price_per_goods
            total_cost = selling_cnt * variable_cost + fixed_cost
            if total_revenue - total_cost <= 0 and before_diff == 0:
                selling_cnt += 1
                before_diff = total_revenue - total_cost
                continue
            elif total_revenue - total_cost <= 0 and abs(before_diff) > abs(total_revenue - total_cost):
                selling_cnt += 1
                before_diff = total_revenue - total_cost
                continue
            elif total_revenue - total_cost <= 0 and abs(before_diff) <= abs(total_revenue - total_cost):
                return -1
            elif total_revenue - total_cost > 0:
                break
        
        return selling_cnt
    

🔗 백준 Online Judge (opens in a new tab)