상세 컨텐츠

본문 제목

백준 2579번

백준 알고리즘 풀이

by 발발개발 2021. 1. 27. 17:04

본문

원본 : www.acmicpc.net/problem/2579

 

2579번: 계단 오르기

계단 오르기 게임은 계단 아래 시작점부터 계단 꼭대기에 위치한 도착점까지 가는 게임이다. <그림 1>과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점

www.acmicpc.net

 

풀이

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {

	public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	public static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

	public static void main(String[] args) throws Exception {

		int n = Integer.parseInt(reader.readLine());
		int input[] = new int[n];
		int calculate[] = new int[n];
		for (int i = 0; i < n; i++) {
			input[i] = Integer.parseInt(reader.readLine());
			calculate[i] = input[i];
		}

		if (n == 1) {
			writer.write(input[0] + "");
		} else if (n == 2) {
			writer.write(input[1] + input[0] + "");
		} else if (n == 3) {
			writer.write(maxValue(input[2] + input[1], input[2] + input[0]) + "");
		} else {
			calculate[1] += calculate[0];
			calculate[2] = maxValue(input[2] + input[0], input[2] + input[1]);

			for (int i = 3; i < n; i++) {
				calculate[i] = input[i] + maxValue(input[i - 1] + calculate[i - 3], calculate[i - 2]);
			}

			writer.write(calculate[calculate.length - 1] + "");
		}

		writer.flush();
	}

	public static int maxValue(int a, int b) {
		if (a >= b) {
			return a;
		} else {
			return b;
		}
	}

}

'백준 알고리즘 풀이' 카테고리의 다른 글

백준 10844번  (0) 2021.01.28
백준 1463번  (0) 2021.01.27
백준 1932번  (0) 2021.01.27
백준 1149번  (0) 2021.01.26
백준 9461번  (0) 2021.01.25

관련글 더보기

댓글 영역