본문 바로가기
Java/알고리즘

[Java] 백준 2839번:설탕배달

by EricJeong 2019. 8. 21.

 

 

 

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int answer = 0;
		
		int div5 = n/5;
		n = n%5;
		int div3 = n/3;
		n = n%3;
		
		while(true) {
			if(div5 < 0) {
				System.out.println(-1);
				break;
			}else if(n == 0){
				answer = div5 + div3;
				System.out.println(answer);
				break;
			}else {
				n += 5;
				n += div3*3;
				div3 = 0;
				div5--;
				div3 = n/3;
				n = n%3;
			}
		}
		
	}
}

댓글