250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 코딩테스트 연습
- count
- sql
- where
- 설계
- List Comprehension
- Len
- 아이엠어바텐더
- 파이썬 슬라이싱
- YOLOv5
- css
- 슬라이싱
- Python3
- 파이썬
- python
- date_format
- 프로젝트
- GIT
- 데이터
- Join
- 단어 공부
- Django
- ORDER BY
- SQL 고득점 Kit
- html
- 프로그래머스
- 파이썬 몫
- map
- 백준
- pcce 기출문제
Archives
- Today
- Total
nan + nan = 2nan
[백준][Python3] #1712. 손익분기점 본문
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
'Python > 백준 알고리즘' 카테고리의 다른 글
[백준][Python3] #11382. 꼬마 정민 (0) | 2024.02.22 |
---|---|
[백준][Python3] #2292. 벌집 (0) | 2023.01.16 |
[백준][Python3] #1316. 그룹 단어 체커 (0) | 2023.01.16 |
[백준][Python3] #2941. 크로아티아 알파벳 (0) | 2023.01.16 |
[백준][Python3] #5622. 다이얼 (0) | 2023.01.15 |
Comments