Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- 건강
- 자바
- 백준
- 프로그래머스
- SWEA
- 시뮬레이션
- 운동기록
- 이분탐색
- oracle
- dfs
- 문제해결
- 문제풀이
- greedy
- 투포인터
- COS PRO
- 문자열
- BOJ
- BFS
- 코테
- db
- math
- 코딩테스트
- binary search
- MySQL
- 알고리즘
- 러닝일지
- SQL
- Java
- DP
- priorityqueue
Archives
- Today
- Total
슈콩
[SWEA] 부분 수열의 합 본문

[문제]
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV7IzvG6EksDFAXB
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
[소스 코드]
// 1차 코드
import java.util.*;
import java.io.*;
public class Solution {
static int n,k,result;
static int[] arr;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for(int tc=1;tc<=T;tc++) {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
arr = new int[n];
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
result = 0;
for(int i=1;i<=n;i++) {
select(0,0,i,0);
}
System.out.println("#"+tc+" "+result);
}
}
private static void select(int cnt,int idx,int max,int sum) {
if(cnt==max) {
if(sum==k) {
result++;
return;
}
}
for(int i=idx;i<n;i++) {
select(cnt+1,i+1,max,sum+arr[i]);
}
}
}
// 최종 코드
import java.util.*;
import java.io.*;
public class Solution {
static int n,k,result;
static int[] arr;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for(int tc=1;tc<=T;tc++) {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
arr = new int[n];
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
result = 0;
select(0,0);
System.out.println("#"+tc+" "+result);
}
}
private static void select(int idx,int sum) {
if(idx==n) {
if(sum==k) {
result++;
}
return;
}
select(idx+1,sum+arr[idx]);
select(idx+1,sum);
}
}'Algorithms > SWEA' 카테고리의 다른 글
| [SWEA] 2일차 - Sum (0) | 2025.11.06 |
|---|---|
| [SWEA] 최장 경로 (0) | 2025.11.05 |
| [SWEA] 5일차 - Magnetic (0) | 2025.11.04 |
| [SWEA] 3일차 - 회문 1 (0) | 2025.11.04 |
| [SWEA] 농작물 수확하기 (0) | 2025.11.03 |