코드와이
[BAEKJOON] 5557. 1학년 본문
문제링크
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]);
}
}
'acmicpc' 카테고리의 다른 글
[BAEKJOON] 2631. 줄 세우기 (0) | 2021.07.07 |
---|---|
[BAEKJOON] 11559. Puyo Puyo (0) | 2021.07.06 |
[BAEKJOON] 2636. 치즈 (0) | 2021.07.06 |
[BAEKJOON] 1068. 트리 (0) | 2021.07.01 |
[BAEKJOON] 9252. LCS2 (0) | 2021.06.30 |