알고리즘
백준 10162번 전자레인지 (JAVA)
박카스마시며코딩
2023. 12. 7. 14:23
https://www.acmicpc.net/problem/10162
10162번: 전자레인지
3개의 시간조절용 버튼 A B C가 달린 전자레인지가 있다. 각 버튼마다 일정한 시간이 지정되어 있어 해당 버튼을 한번 누를 때마다 그 시간이 동작시간에 더해진다. 버튼 A, B, C에 지정된 시간은
www.acmicpc.net
저는 그리디를 통해 문제를 해결하였습니다.
먼저 5분으로 나누고, 1분 , 10초 나누어 떨어지는지를 확인하고 나누어 떨어지지 않으면 -1을 출력, 그렇지 않다면 각각의 몫을 출력하여 문제를 풀었습니다.
package BOJ.greedy;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BOJ_10162_2 {
private static final int ONE_MINUTE = 60;
private static final int[] TIME = {5 * ONE_MINUTE, ONE_MINUTE, 10};
private static final int[] NOT_FOUND = {-1};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] result = cal(n);
for(int num: result){
System.out.print(num + " ");
}
}
private static int[] cal(int money){
int[] result = new int[3];
for(int i = 0 ; i < 3 ; i++){
result[i] = money/TIME[i];
money %= TIME[i];
}
if(money != 0){
return NOT_FOUND;
}else{
return result;
}
}
}