acmicpc

[BAEKJOON] 5557. 1학년

코드와이 2021. 7. 6. 16:01

 

문제링크

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

 

5557번: 1학년

상근이가 1학년 때, 덧셈, 뺄셈을 매우 좋아했다. 상근이는 숫자가 줄 지어있는 것을 보기만 하면, 마지막 두 숫자 사이에 '='을 넣고, 나머지 숫자 사이에는 '+' 또는 '-'를 넣어 등식을 만들며 놀

www.acmicpc.net

 

package acmicpc.Gold5;

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

public class firstGrade {

	static int n, num[];
	static long cnt[][];
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		n = Integer.parseInt(br.readLine());
		num = new int[n];
		cnt = new long[21][n - 1];
		
		st = new StringTokenizer(br.readLine());
		for(int i = 0 ; i < n ; i++) num[i] = Integer.parseInt(st.nextToken());
		
		cnt[num[0]][0] = 1;
		
		for(int i = 1 ; i < n - 1; i++) {
			for(int j = 0 ; j < 21 ; j++) {
				if(cnt[j][i - 1] > 0) {
					
					// 뺄셈
					if(j - num[i] >= 0) {
						cnt[j - num[i]][i] += cnt[j][i - 1];
					}
					
					// 덧셈
					if(j + num[i] <= 20) {
						cnt[j + num[i]][i] += cnt[j][i - 1];
					}
				}
			}
		}
		System.out.println(cnt[num[n-1]][n-2]);
	}
}