https://www.acmicpc.net/problem/11047
그리디 알고리즘을 활용한 단순한 문제이다.
가장 큰수 부터 나눠지는대로 나눠서 몫을 cnt 값에 더해주기만 하면 끝이다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] agrs) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int cnt = 0;
int[] A = new int[N];
for (int i = 0; i < N; i++)
A[i] = Integer.parseInt(br.readLine());
for (int i = N - 1; 0 <= i; i--) {
int quotient = K / A[i]; // 몫
if (quotient == 0)
continue;
else {
K = K - A[i] * quotient;
cnt += quotient;
}
}
System.out.println(cnt);
}
}
'프로그래밍 & IT > Algorithm' 카테고리의 다른 글
[알고리즘] 백준 10026번 적록색약 :: 우유 (0) | 2022.06.19 |
---|---|
[알고리즘] 백준 4963번 섬의개수 :: 우유 (0) | 2022.06.19 |
[알고리즘] 백준 4949번 균형잡힌세상 :: 우유 (0) | 2022.06.09 |
[알고리즘] 백준 2477번 참외밭 :: 우유 (0) | 2022.06.09 |
[알고리즘] 백준 7562번 나이트의 이동 :: 우유 (0) | 2022.04.18 |