Study & Project ✏️/알고리즘 📋

백준[JAVA] 1712.손익분기점 - 자바

JM 2022. 9. 10. 19:19
반응형

📖 문제

📃 코드

1. for문

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static int BEP(int stabelPrice, int variablePrice, int productPrice) {
        for (int i = 1;; i++) {
            // BEP Achievement
            if (stabelPrice / (productPrice - variablePrice) < i)
                return i;
        }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s[] = br.readLine().split(" ");
        int stabelPrice = Integer.parseInt(s[0]);
        int variablePrice = Integer.parseInt(s[1]);
        int productPrice = Integer.parseInt(s[2]);

        int result = BEP(stabelPrice, variablePrice, productPrice);

        System.out.println(result);
    }
}

2. BEP

🐞 런타임 에러 (/ by zero)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static int BEP(int stabelPrice, int variablePrice, int productPrice) {
        if(productPrice - variablePrice == 0)
            return -1;
        // BEP Achievement
        int result = (stabelPrice / (productPrice - variablePrice)) + 1;
        if(result <= 0)
            return -1;
        return result;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s[] = br.readLine().split(" ");
        int stabelPrice = Integer.parseInt(s[0]);
        int variablePrice = Integer.parseInt(s[1]);
        int productPrice = Integer.parseInt(s[2]);

        int result = BEP(stabelPrice, variablePrice, productPrice);

        System.out.println(result);
    }
}

✏️ Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static int BEP(int stabelPrice, int variablePrice, int productPrice) {
        // Exception divided 0 error
        if(productPrice - variablePrice == 0)
            return -1;
        // BEP Achievement
        int result = (stabelPrice / (productPrice - variablePrice)) + 1;
        if(result <= 0)
            return -1;
        return result;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s[] = br.readLine().split(" ");
        int stabelPrice = Integer.parseInt(s[0]);
        int variablePrice = Integer.parseInt(s[1]);
        int productPrice = Integer.parseInt(s[2]);

        int result = BEP(stabelPrice, variablePrice, productPrice);

        System.out.println(result);
    }
}

🔗 링크

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

 

1712번: 손익분기점

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

www.acmicpc.net