728x90
오늘의 멘트가 너무 좋다ㅎㅎ 3일이나 했다!

오늘의 문제: 박스 채우기
오늘의 문제 코드
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int w = sc.nextInt();
int h = sc.nextInt();
int n = sc.nextInt();
int[][] blocks = new int[n][2];
for (int i = 0; i < n; i++) {
blocks[i][0] = sc.nextInt(); // 블록 크기의 지수
blocks[i][1] = sc.nextInt(); // 블록 개수
}
System.out.println(fillBox(l, w, h, blocks));
}
public static int fillBox(int l, int w, int h, int[][] blocks) {
Arrays.sort(blocks, (a, b) -> b[0] - a[0]); // 블록 크기 순으로 정렬
long totalCount = 0;
long volume = (long) l * w * h; // 상자의 전체 부피
long usedVolume = 0;
for (int i = 0; i < blocks.length; i++) {
long size = ____; // 블록의 한 변 크기
int count = blocks[i][1];
if (volume <= usedVolume) {
break; // 상자를 이미 채웠다면 종료
}
long maxCount = ____; // 현재 크기로 채울 수 있는 최대 개수
maxCount -= usedVolume / (size * size * size); // 이미 채워진 공간 제외
long useCount = Math.min(count, maxCount);
usedVolume += useCount * (size * size * size);
totalCount += useCount;
}
// 상자를 다 채웠는지 확인
if (____) {
return (int) totalCount;
} else {
return -1;
}
}
}
1. long size = ____; // 블록의 한 변 크기
문제에 2의 진수로 표현된다고 나와있어서 익숙한 Math.pow(2, )를 선택했다. 그렇지만
Math.pow()는 double로만 표현 가능.. long형식인 변수 size에는 안들어가진다..


그래서 답은 d!
2. long maxCount = ____; // 현재 크기로 채울 수 있는 최대 개수

3. // 상자를 다 채웠는지 확인
if (____) {

오늘두 1번 ❌, 2번 ⭕️, 3번 ⭕️ ㅠㅠ
내일은 다 맞아보겠다ㅏㅏ!

'Algorithm > 탭고리즘' 카테고리의 다른 글
[탭고리즘] 2일차 (0) | 2025.02.12 |
---|---|
[탭고리즘] 1일차 (0) | 2025.02.12 |
[탭고리즘] 탭고리즘? (0) | 2025.02.12 |
728x90
오늘의 멘트가 너무 좋다ㅎㅎ 3일이나 했다!

오늘의 문제: 박스 채우기
오늘의 문제 코드
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int w = sc.nextInt();
int h = sc.nextInt();
int n = sc.nextInt();
int[][] blocks = new int[n][2];
for (int i = 0; i < n; i++) {
blocks[i][0] = sc.nextInt(); // 블록 크기의 지수
blocks[i][1] = sc.nextInt(); // 블록 개수
}
System.out.println(fillBox(l, w, h, blocks));
}
public static int fillBox(int l, int w, int h, int[][] blocks) {
Arrays.sort(blocks, (a, b) -> b[0] - a[0]); // 블록 크기 순으로 정렬
long totalCount = 0;
long volume = (long) l * w * h; // 상자의 전체 부피
long usedVolume = 0;
for (int i = 0; i < blocks.length; i++) {
long size = ____; // 블록의 한 변 크기
int count = blocks[i][1];
if (volume <= usedVolume) {
break; // 상자를 이미 채웠다면 종료
}
long maxCount = ____; // 현재 크기로 채울 수 있는 최대 개수
maxCount -= usedVolume / (size * size * size); // 이미 채워진 공간 제외
long useCount = Math.min(count, maxCount);
usedVolume += useCount * (size * size * size);
totalCount += useCount;
}
// 상자를 다 채웠는지 확인
if (____) {
return (int) totalCount;
} else {
return -1;
}
}
}
1. long size = ____; // 블록의 한 변 크기
문제에 2의 진수로 표현된다고 나와있어서 익숙한 Math.pow(2, )를 선택했다. 그렇지만
Math.pow()는 double로만 표현 가능.. long형식인 변수 size에는 안들어가진다..


그래서 답은 d!
2. long maxCount = ____; // 현재 크기로 채울 수 있는 최대 개수

3. // 상자를 다 채웠는지 확인
if (____) {

오늘두 1번 ❌, 2번 ⭕️, 3번 ⭕️ ㅠㅠ
내일은 다 맞아보겠다ㅏㅏ!

'Algorithm > 탭고리즘' 카테고리의 다른 글
[탭고리즘] 2일차 (0) | 2025.02.12 |
---|---|
[탭고리즘] 1일차 (0) | 2025.02.12 |
[탭고리즘] 탭고리즘? (0) | 2025.02.12 |