SSAFY/SWEA

[SWEA] 2817 : 부분 수열의 합

믕비 2023. 5. 3. 18:47

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV7IzvG6EksDFAXB&categoryId=AV7IzvG6EksDFAXB&categoryType=CODE&problemTitle=&orderBy=SUBMIT_COUNT&selectCodeLang=JAVA&select-1=3&pageSize=10&pageIndex=2 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

내 코드

메모리 : 20180 KB

시간 : 200 ms

코드길이 : 1322 B

 

[내 코드]

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

public class Solution {
	static int[] arr;
	static int N;
	static int K;
	static int result;

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st;
		
		int T = Integer.parseInt(br.readLine());
		for(int t = 1; t <= T; t++) {
			sb.append('#').append(t).append(' ');
			st = new StringTokenizer(br.readLine());
			N = Integer.parseInt(st.nextToken());
			K = Integer.parseInt(st.nextToken());
			result = 0;
			arr = new int[N];
			boolean[] selected = new boolean[N];
			st = new StringTokenizer(br.readLine());
			for(int n = 0; n < N; n++) {
				arr[n] = Integer.parseInt(st.nextToken());
			}
			
			for(int n = 0; n < N; n++) {
				selected[n] = true;
				search(n, 0, selected);
			}
			sb.append(result).append('\n');
		}
		System.out.print(sb);
	}
	
	public static void search(int index, int sum, boolean[] selected) {
		sum += arr[index];

		if(sum == K)
			result++;
		
		for(int i = index + 1; i < N; i++) {
			if(!selected[i]) {
				selected[i] = true;
				search(i, sum, selected);
				selected[i] = false;
			}
		}
		
		return;
	}

}

'SSAFY > SWEA' 카테고리의 다른 글

[SWEA] 3750 : Digit sum  (0) 2023.05.03
[SWEA] 11315 : 오목판정  (0) 2023.05.03
[SWEA] 4408 : 자기 방으로 돌아가기  (0) 2023.05.03
[SWEA] 6485 : 삼성시의 버스 노선  (0) 2023.05.02
[SWEA] 9229 : 한빈이와 spot Mart  (0) 2023.05.02