JAVA
자바 손익분기점, 이동횟수 구하기 (백준 1712, 1011)
클로키
2020. 6. 18. 19:23
< 백준 1712번 손익분기점 >
- 회사의 고정비용과 물건 하나 당 가변비용, 판매가격이 주어질 때 손익분기점을 넘어 수익이 발생하기 위한 최소 판매수량을 찾는 문제.
- 반복문을 돌려 수익이 발생하는 시점을 알아낼 수도 있지만 판매수량을 찾는 수식만 세우면 반복문 없이 쉽게 풀 수 있다.
import java.io.*;
import java.util.*;
class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer sc = new StringTokenizer(br.readLine(), " ");
int fixed = Integer.parseInt(sc.nextToken());
int cost = Integer.parseInt(sc.nextToken());
int price = Integer.parseInt(sc.nextToken());
int count = (price<=cost) ? -1 : (fixed/(price-cost)+1);
System.out.println(count);
}
}
< 백준 1011번 Fly me to the Alpha Centuari >
- x에서 y로 가는 데 필요한 이동 횟수를 구하는 문제.
- n번째의 이동거리는 n-1번째의 이동거리와 같거나 ±1만 가능하다.
- 규칙을 찾아보면, 횟수가 2씩 늘어날수록 이동거리 증가량은 1씩 증가한다. 이 증가량을 adder로 두고 해당 거리가몇 번의 add로 완성되는지 찾아내었다.
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int i = 0; i<num; i++){
int x = sc.nextInt();
int y = sc.nextInt();
sc.nextLine();
int result = 0;
int adder = 1;
int dif = y-x;
while(dif>0){
dif -= adder;
result++;
if(dif>0){
dif -= adder++;
result++;
}
}
System.out.println(result);
}
}
}