java
입력으로 종이의 가로 세로 길이, 그리고 잘라야할 점선들이 주어질 때, 가장 큰 종이 조각의 넓이가 몇 ㎠인지를 구하는 프로그램을 작성하시오.
ArrayList 를 활용해 문제를 풀었다.
비슷한 문제를 더 풀어볼 계획이다.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int width = sc.nextInt();
int height = sc.nextInt();
int n = sc.nextInt();
ArrayList<Integer> horizontalCuts = new ArrayList<>();
ArrayList<Integer> verticalCuts = new ArrayList<>();
for (int i = 0; i < n; i++) {
int direction = sc.nextInt();
int cut = sc.nextInt();
if (direction == 0) {
horizontalCuts.add(cut);
} else {
verticalCuts.add(cut);
}
}
horizontalCuts.add(0);
horizontalCuts.add(height);
verticalCuts.add(0);
verticalCuts.add(width);
Collections.sort(horizontalCuts);
Collections.sort(verticalCuts);
int maxH = 0, maxV = 0;
for (int i = 1; i < horizontalCuts.size(); i++) {
maxH = Math.max(maxH, horizontalCuts.get(i) - horizontalCuts.get(i - 1));
}
for (int i = 1; i < verticalCuts.size(); i++) {
maxV = Math.max(maxV, verticalCuts.get(i) - verticalCuts.get(i - 1));
}
System.out.println(maxH * maxV);
}
}
Java들어가며 소프트웨어를 개발할 때 메모리 관리 방식은 프로그램의 안정성과 성능을 좌우하는 핵심 요소입니다. 특히 자바스크립트,…
들어가며 소프트웨어 개발자는 코드가 어떻게 실행되는지 정확히 이해해야 할 필요가 있습니다. 우리가 작성한 프로그램은 결국…
서론 현대 웹 애플리케이션 아키텍처에서 웹 서버(Web Server) 와 웹 애플리케이션 서버(WAS, Web Application Server)…
HTTP 헤더(Header)란? HTTP(Header)는 클라이언트와 서버 간에 교환되는 메타데이터로, 요청(Request)과 응답(Response)에 부가적인 정보를 실어 나르는 역할을…
Readable Code: 읽기 좋은 코드를 작성하는 사고법Practical Testing: 실용적인 테스트 가이드 강의와 함께한 인프런 워밍업 클럽…
Readable Code: 읽기 좋은 코드를 작성하는 사고법Practical Testing: 실용적인 테스트 가이드 강의와 함께한 인프런 워밍업 클럽…