Skip to content

Commit 2fdc4c5

Browse files
committed
[Gold V] Title: 1학년, Time: 100 ms, Memory: 14044 KB -BaekjoonHub
1 parent 2ac39e0 commit 2fdc4c5

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
int n = Integer.parseInt(br.readLine());
11+
int [] arr = new int[n];
12+
long [][] dp = new long[n][21];
13+
14+
String[] s = br.readLine().split(" ");
15+
16+
for(int i=0; i<n; i++){
17+
arr[i] = Integer.parseInt(s[i]);
18+
}
19+
20+
dp[0][arr[0]]=1;
21+
22+
int plus;
23+
int minus;
24+
for(int i=1; i<n-1; i++){
25+
for(int j=0; j<=20; j++){
26+
if(dp[i-1][j]!=0){
27+
plus = j+arr[i];
28+
minus = j-arr[i];
29+
if(plus>=0 && plus<=20){
30+
dp[i][plus]+=dp[i-1][j];
31+
}
32+
if(minus>=0 && minus<=20){
33+
dp[i][minus]+=dp[i-1][j];
34+
}
35+
}
36+
}
37+
}
38+
39+
System.out.println(dp[n-2][arr[n-1]]);
40+
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Gold V] 1학년 - 5557
2+
3+
[문제 링크](https://www.acmicpc.net/problem/5557)
4+
5+
### 성능 요약
6+
7+
메모리: 14044 KB, 시간: 100 ms
8+
9+
### 분류
10+
11+
다이나믹 프로그래밍
12+
13+
### 제출 일자
14+
15+
2025년 3월 17일 21:18:41
16+
17+
### 문제 설명
18+
19+
<p>상근이가 1학년 때, 덧셈, 뺄셈을 매우 좋아했다. 상근이는 숫자가 줄 지어있는 것을 보기만 하면, 마지막 두 숫자 사이에 '='을 넣고, 나머지 숫자 사이에는 '+' 또는 '-'를 넣어 등식을 만들며 놀고 있다. 예를 들어, "8 3 2 4 8 7 2 4 0 8 8"에서 등식 "8+3-2-4+8-7-2-4-0+8=8"을 만들 수 있다.</p>
20+
21+
<p>상근이는 올바른 등식을 만들려고 한다. 상근이는 아직 학교에서 음수를 배우지 않았고, 20을 넘는 수는 모른다. 따라서, 왼쪽부터 계산할 때, 중간에 나오는 수가 모두 0 이상 20 이하이어야 한다. 예를 들어, "8+3+2-4-8-7+2+4+0+8=8"은 올바른 등식이지만, 8+3+2-4-8-7이 음수이기 때문에, 상근이가 만들 수 없는 등식이다.</p>
22+
23+
<p>숫자가 주어졌을 때, 상근이가 만들 수 있는 올바른 등식의 수를 구하는 프로그램을 작성하시오.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 숫자의 개수 N이 주어진다. (3 ≤ N ≤ 100) 둘째 줄에는 0 이상 9 이하의 정수 N개가 공백으로 구분해 주어진다.</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 상근이가 만들 수 있는 올바른 등식의 개수를 출력한다. 이 값은 2<sup>63</sup>-1 이하이다.</p>
32+

0 commit comments

Comments
 (0)