티스토리 뷰
문제 출처:www.acmicpc.net/problem/11047
11047번: 동전 0
첫째 줄에 N과 K가 주어진다. (1 ≤ N ≤ 10, 1 ≤ K ≤ 100,000,000) 둘째 줄부터 N개의 줄에 동전의 가치 Ai가 오름차순으로 주어진다. (1 ≤ Ai ≤ 1,000,000, A1 = 1, i ≥ 2인 경우에 Ai는 Ai-1의 배수)
www.acmicpc.net
풀이
다행스럽게도 입력되는 금액의 가치들이 오름차순으로 입력된다. 굳이 정렬을 해줄 필요가 없다.
큰 가치부터 내림차순으로 내려오면서 K랑 나눴을 때 몫이 0보다 크면, 몫을 계속 ans변수에 더해줬다.
그리고 K는 나머지로 바꿔주면서 쭉 진행하면 정답이 나온다.
코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package algorithm; | |
import java.util.Scanner; | |
public class B11047_동전0 { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int N = sc.nextInt(); | |
int K = sc.nextInt(); | |
int ans = 0; | |
int coins[] = new int[N]; | |
for (int i = 0; i < N; i++) { | |
coins[i] = sc.nextInt(); | |
} | |
for (int i = coins.length - 1; i >= 0; i--) { | |
int divided = coins[i]; | |
if (K / divided > 0) { | |
ans += (K / divided); | |
K = K % divided; | |
} | |
} | |
System.out.println(ans); | |
} | |
} |
'알고리즘' 카테고리의 다른 글
백준 - 암호 만들기 (0) | 2021.03.26 |
---|---|
백준 - 외판원 순회2 (0) | 2021.03.14 |
프로그래머스 -단속카메라 (0) | 2021.02.04 |
프로그래머스 - 가장 먼 노드 (0) | 2021.02.03 |
<프로그래머스> 단어 변환 (0) | 2021.01.30 |