반응형
📖 문제
📃 코드
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
'Study & Project ✏️ > 알고리즘 📋' 카테고리의 다른 글
백준[JAVA] 2745.진법 변환 - 자바 (0) | 2022.09.12 |
---|---|
백준[JAVA] 2750.수 정렬하기 - 자바 (0) | 2022.09.12 |
백준[JAVA] 2355.시그마 - 자바 (0) | 2022.09.11 |
백준[JAVA] 2292.벌집 - 자바 (0) | 2022.09.11 |
백준[JAVA] 1964.오각형, 오각형, 오각형... - 자바 (0) | 2022.09.10 |