본문 바로가기

개발/알고리즘

그리디 알고리즘 백준 5585

백준 5585번

 

이 문제는 굉장히 쉬운편이었습니다...

그냥 다중 if문으로 해도 해결이 되는 쉬운 문제였어요.

N으로 주어진 수를 1000에서 빼고 뺼 수 있는 수를 차례대로 빼면 완성이됩니다.

public class Main {
	private static int N,res;

	public static void main(String[] args) throws IOException{
		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		res = 0;
		N = 1000-N;
		while(N != 0) {
			if(N>=500) {
				res++;
				N = N-500;
			}else if(N>=100) {
				res++;
				N = N-100;
			}else if(N >=50) {
				res++;
				N = N-50;
			}else if(N>=10) {
				res++;
				N = N-10;
			}else if(N>=5) {
				res++;
				N = N-5;
			}else {
				res++;
				N = N-1;
			}
		}
		System.out.println(res);
	}
}