nan + nan = 2nan

[백준][Python3] #1712. 손익분기점 본문

Python/백준 알고리즘

[백준][Python3] #1712. 손익분기점

2nan 2023. 1. 16. 22:02
728x90


코드 입력

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

print(break_even_point())

 

 


출처 : 백준 Online Judge

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

 

1712번: 손익분기점

월드전자는 노트북을 제조하고 판매하는 회사이다. 노트북 판매 대수에 상관없이 매년 임대료, 재산세, 보험료, 급여 등 A만원의 고정 비용이 들며, 한 대의 노트북을 생산하는 데에는 재료비와

www.acmicpc.net

 

Comments