Algorithm/Baekjoon

[백준] 1712번 : 손익분기점 - Java

unknownomad 2022. 3. 22. 19:00

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

 

1712번: 손익분기점

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

www.acmicpc.net



주의점

  • 처음으로 이익이 발생하는 지점의 판매량 출력
  • A, B, C = 자연수

알고리즘

  • 고정 비용(불변 비용) = A (상품 개수와 상관 없이 항상 동일함)
  • 가변 비용 = B
  • 상품 가격 = C
  • 상품 개수 = n
n x 상품 가격(C) > 고정 비용(A) + (n x 가변 비용(B))

n x C = 총 수입

//총 수입 = 총 비용
n x C = A + (n x B)

nC = A + nB
nC - nB = A
n(C - B) = A

          A
∴ n = -------  = 총 수익 = 총 비용(이익 발생하는 지점 X)
        C - B
//이익이 발생하는 지점(손익분기점)
   A
------- + 1 > 0
 C - B
 
 ∴ 손익분기점 = (A / (C - B)) + 1
 
//이익이 발생하지 않는다면 -1 출력
   A
------- + 1 ≤ 0
 C - B
 
 //위 공식에서 C - B 가 0 이하 ~ 음수가 나오면 됨
 C - B ≤ 0
 
 ∴ C ≤ B

풀이

1. Scanner

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);

        int A = in.nextInt(); // 불변 비용
        int B = in.nextInt(); // 가변 비용
        int C = in.nextInt(); // 상품 가격
        
        if (C <= B) {
            System.out.println("-1");
        } else {
            System.out.println(( A / (C - B)) + 1);
        }
    }
}

 

2. BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Main {
    public static void main(String[] args) throws IOException {
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //br.readLine(): 문자열로 받음
        //StringTokenizer: 공백 단위로 문자열 분리
        StringTokenizer st = new StringTokenizer(br.readLine());

        //st.nextToken(): String
        //String to int
        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());

        if (C <= B) {
            System.out.println("-1");
        } else {
            System.out.println(( A / (C - B)) + 1);
        }
    }
}

성능

  • BufferedReader > Scanner

 


출처 : https://st-lab.tistory.com/71