Java/Java 알고리즘

백준 25278번 Terraforming JAVA 구현해보기

kimc 2022. 7. 29. 23:29

```

백준 25278번 Terraforming JAVA 구현해보기

```

이번 글을 통해 배워갈 내용

  1. 백준 25278번 Terraforming JAVA 풀이

 


https://www.acmicpc.net/problem/25278

 

25278번: Terraforming

On the first line, the number $n\in\{1,\ldots, 315\}$ of environmental changes on Mars. Each of the following $n$ lines consists of a parameter (oxygen, ocean, or temperature), followed by an integer. The change for oxygen and oceans is in percentage point

www.acmicpc.net

 

백준 25278번 테라포밍은

난이도 브론즈 등급의 문제로서

 

화성의

현재 주어진 바다 비율, 산소농도, 기온이

0%, 0%, -30도 일 때

 

각 명령어에 맞춰서

주어진 조건의 변화를 준다고 가정하고

 

모든 명령이 수행된 후

바다 비율, 산소농도, 기온이

9%, 14%, +8도 이상이 모두 충족되는 경우

liveable을 출력하고 아닌 경우 not liveable을 출력하면 되는 문제입니다.

 


30분 정도 위에 링크를 방문하셔서 풀어보시고

안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.


명령에 따라서 화성의 조건을 변경해주고

최종 조건에 결괏값이 조건에 부합하는지 확인해서 출력하는 문제입니다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static MarsCondition mc;

    public static void main(String[] args) throws IOException {
        mc = new MarsCondition(0, 0, -30);
        final int commandCount = Integer.parseInt(br.readLine());
        for (int i = 0; i < commandCount; i++) {
            final String[] input = br.readLine().split(" ");
            final String commandType = input[0];
            final int commandVal = Integer.parseInt(input[1]);
            executeCommand(commandType, commandVal);
        }
        System.out.print(isMarsLiveable() ? "liveable" : "not liveable");
    }

    private static boolean isMarsLiveable() {
        final int MIN_OCEAN_COVERAGE = 9;
        final int MIN_OXYZEN = 14;
        final int MIN_TEMPERATURE = 8;

        boolean isLiveable = true;
        if (mc.oceanCoverage < MIN_OCEAN_COVERAGE) isLiveable = false;
        if (mc.oxygenLevel < MIN_OXYZEN) isLiveable = false;
        if (mc.temperatureLevel < MIN_TEMPERATURE) isLiveable = false;
        return isLiveable;
    }

    private static void executeCommand(String commandType, int commandVal) {
        switch (commandType) {
            case "temperature": {
                mc.temperatureLevel += commandVal;
                break;
            }
            case "oxygen": {
                mc.oxygenLevel += commandVal;
                break;
            }
            case "ocean": {
                mc.oceanCoverage += commandVal;
                break;
            }
        }
    }

    private static class MarsCondition {
        int oceanCoverage;
        int oxygenLevel;
        int temperatureLevel; // in Celsius

        public MarsCondition(int oceanCoverage, int oxygenLevel, int temperatureLevel) {
            this.oceanCoverage = oceanCoverage;
            this.oxygenLevel = oxygenLevel;
            this.temperatureLevel = temperatureLevel;
        }
    }
}

// https://codemasterkimc.tistory.com

 

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

728x90