-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[D3] Title: 부분 수열의 합, Time: 2,414 ms, Memory: 20,396 KB -BaekjoonHub
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# [D3] 부분 수열의 합 - 2817 | ||
|
||
[문제 링크](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV7IzvG6EksDFAXB) | ||
|
||
### 성능 요약 | ||
|
||
메모리: 20,396 KB, 시간: 2,414 ms, 코드길이: 1,542 Bytes | ||
|
||
### 제출 일자 | ||
|
||
2023-11-18 13:43 | ||
|
||
|
||
|
||
> 출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
public class Solution { | ||
|
||
static int n, k; | ||
static int ans = 0; | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(br.readLine()); | ||
|
||
int cnt = 1; | ||
|
||
while(t-->0) { | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
n = Integer.parseInt(st.nextToken()); | ||
k = Integer.parseInt(st.nextToken()); | ||
|
||
int arr[] = new int[n]; | ||
ans = 0; | ||
st = new StringTokenizer(br.readLine()); | ||
for(int i=0; i<n; i++) | ||
arr[i] = Integer.parseInt(st.nextToken()); | ||
|
||
for(int i=1; i<=arr.length; i++) { | ||
comb(arr, new boolean[n], 0, i); | ||
} | ||
|
||
System.out.println("#" + cnt++ + " " + ans); | ||
|
||
} | ||
} | ||
|
||
public static void comb(int arr[], boolean visited[], int start, int r) { | ||
if(r == 0) { | ||
int sum = 0; | ||
|
||
for(int i=0; i<arr.length; i++) { | ||
if(visited[i]) | ||
sum += arr[i]; | ||
} | ||
if(sum == k) ans ++; | ||
return; | ||
} | ||
|
||
for(int i=start; i<arr.length; i++) { | ||
if(!visited[i]){ | ||
visited[i] = true; | ||
comb(arr, visited, i + 1, r-1); | ||
visited[i] = false; | ||
} | ||
} | ||
} | ||
|
||
} |