Mini

2839번: 설탕배달 본문

Algorithm/boj

2839번: 설탕배달

Mini_96 2022. 8. 16. 13:21

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

 

2839번: 설탕 배달

상근이는 요즘 설탕공장에서 설탕을 배달하고 있다. 상근이는 지금 사탕가게에 설탕을 정확하게 N킬로그램을 배달해야 한다. 설탕공장에서 만드는 설탕은 봉지에 담겨져 있다. 봉지는 3킬로그

www.acmicpc.net

 

1.최대사용가능 5의 수 구하기 == 기준

2. 사용가능5의 갯수를 1씩 줄여가며 최대~0까지 반복

 

- 함수

1.사용한5만큼 N에서 빼줌

2. 나머지가지고 3씩빼면서 카운트++

3. N이 0이면 가능한것임. possible=true, 정답갱신

 

- 출력

1. possibe? : answer : -1

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

public class Main_2839_유동훈 {

	static int N;
	//static int five;
	//static int three;
	static int answer=Integer.MAX_VALUE;
	static boolean possible=false;
	
	static void sol(int five_use, int n)
	{
		//int five=0;
		int three_use=0;
		
		
		for(int i=0;i<five_use;++i)
		{
			n=n-5;
			//five++;
		}
		
		while(n>0)
		{
			n=n-3;
			three_use++;
		}
		
		if(n==0) 
		{
			answer=Math.min(answer,three_use+five_use);
			possible=true;
		}
		//else return -1;
		
		
		//sol(five_use-1);
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		N=Integer.parseInt(br.readLine());
		
		
		
		int max_five_use=N/5;
		//sol(five_use,N);
		//기준 : 5의갯수
		//5의갯수를 최대~0까지 반복
		for(int i=max_five_use;i>=0;--i)
		{
			sol(i,N);
		}
		
		
		
		StringBuilder sb= new StringBuilder();
		if(possible)	sb.append(answer);
		else	sb.append(-1);
		System.out.println(sb);
	}

}

'Algorithm > boj' 카테고리의 다른 글

백준 1697 숨바꼭질 //BFS 큐  (0) 2022.08.20
17375: 캐슬디펜스  (0) 2022.08.19
백준: 11047 동전 0  (0) 2022.08.12
백준 : 15686 치킨배달  (0) 2022.08.12
11286번: 절대값 힙  (0) 2022.08.12