백준알고리즘
https://www.acmicpc.net/problem/9465
풀이 설명은 http://stack07142.tistory.com/42 에서 보기를..
import java.util.Scanner; public class Main { public static void main(String args[]) throws Exception { Scanner scan = new Scanner(System.in); int testCase = Integer.parseInt(scan.nextLine().trim()); for(int i=0; i < testCase; i++) { int count = Integer.parseInt(scan.nextLine().trim()); int inputArray[][] = new int[2][count+1]; int memoArray[][] = new int[2][count+1]; for(int ai=0; ai<2; ai++) { for(int j=1; j<=count; j++) { if(j < count) { inputArray[ai][j] = Integer.parseInt(scan.next()); continue; } inputArray[ai][j] = Integer.parseInt(scan.nextLine().trim()); } } memoArray[0][1] = inputArray[0][1]; memoArray[1][1] = inputArray[1][1]; for(int ci=2; ci <= count; ci++) { memoArray[0][ci] = inputArray[0][ci] + Math.max(memoArray[1][ci-1], memoArray[1][ci-2]); memoArray[1][ci] = inputArray[1][ci] + Math.max(memoArray[0][ci-1], memoArray[0][ci-2]); } System.out.println(Math.max(memoArray[0][count], memoArray[1][count])); }
scan.close(); } }
'알고리즘 및 자료구조 > 문제' 카테고리의 다른 글
백준알고리즘 10844번 쉬운 계단 수 (0) | 2018.05.10 |
---|---|
백준알고리즘 2902번 KMP는 왜 KMP일까? (0) | 2018.05.10 |
백준알고리즘 10989번 수 정렬하기 3 (0) | 2018.05.10 |
백준알고리즘 2455번 지능형 기차 (0) | 2018.05.10 |
백준알고리즘 11057번 오르막 수 (0) | 2018.05.10 |