탐욕 알고리즘을 이용한 거스름돈

Haechan Kim·2021년 10월 26일
0

알고리즘

목록 보기
19/28
public class Main
{// 탐욕적 알고리즘으로 거스름돈 최소 동전으로 주기
 // but 12원 이런 동전 나오면 쓸 수 없음.
    public static int func(int m, int[] d, int n)
    { // 도전 종류, 동전 값, 거스름돈
        int count = 0;
        int i = 0;
        while (n > 0 && i<m)
        {
            count += n/d[i];
            n = n % d[i];
            i++;
        }
        return count;
    }
    
    public static void main(String[] args)
    {
    	int m = 5;
        int[] d = {500, 100, 50, 10, 1};
        int n = 654;
        System.out.println(func(m, d, n));
    }
}

0개의 댓글

관련 채용 정보