일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 피보나치
- 프로그래머스
- 데이터베이스
- 다이나믹프로그래밍
- db
- DP
- join
- mybatis
- 그리디알고리즘
- 정렬
- 우선순위큐
- 깊이우선탐색
- Database
- 코테
- 이펙티브자바
- DFS
- IntelliJ
- Greedy
- Effective Java
- 탐욕법
- 너비우선탐색
- BFS
- springboot
- SQL
- select
- java
- 백준
- mariaDB
- 알고리즘
- Spring
Archives
- Today
- Total
땀두 블로그
[백준] 11047번 - 동전 0 본문


그리디 알고리즘 중 거스름돈 알고리즘을 이용한 간단한 문제이다. 가장 큰 금액부터 비교해가며 금액을 제외하고, 그 카운트를 출력해주어서 해결하였다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class p11047 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int[] ary = new int[a];
for (int i = 0; i < a; i++) {
ary[i] = Integer.parseInt(br.readLine());
}
int cnt = 0;
for (int i = a - 1; i >= 0; i--) {
while (b >= ary[i]) {
if (b >= ary[i]) {
cnt++;
b -= ary[i];
}
}
}
System.out.println(cnt);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 11021번 - A+B-7 (0) | 2022.03.22 |
---|---|
[백준] 15552번 - 빠른 A+B (0) | 2022.03.22 |
[백준] 2178번 - 미로 탐색 (0) | 2022.03.22 |
[백준] 11659번 - 구간 합 구하기 4 (0) | 2022.03.22 |
[백준] 11727번 - 2*n 타일링 2 (0) | 2022.03.22 |
Comments