์ตœ๋Œ€ 1 ๋ถ„ ์†Œ์š”

๐Ÿ“ [S2_1182] ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ

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

public class Main {
	static StringTokenizer st;
	static int N,S;
	// ํ•ฉ
	static int sum = 0;
	// ์นด์šดํŠธ ( ๊ฒฐ๊ณผ๊ฐ’ )
	static int res = 0;
	static int[] arr;
	static boolean[] isCheck;
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		st = new StringTokenizer(br.readLine(), " ");
		N = Integer.parseInt(st.nextToken());
		S = Integer.parseInt(st.nextToken());
		
		arr = new int[N];
		isCheck = new boolean[N];
		
		
		// ๋ฐฐ์—ด ์ž…๋ ฅ
		st = new StringTokenizer(br.readLine(), " ");
		for(int i=0; i<N; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}
		
		dfs(0);
		// ๋‹ค false์ธ  ๊ฒฝ์šฐ
		if(S == 0) {
			res--;
		}
		
		sb.append(res);
		System.out.println(sb);
	}
	
	static void dfs(int current) {
		if(current == N) {
			for(int i=0; i<N; i++) {
				if(isCheck[i]) {
					sum += arr[i];
				}
				else {
					continue;
				}
			}
			if(sum == S) {
				res++;
			}
			sum = 0;
			return;
		}
		isCheck[current] = true;
		dfs(current+1);
		
		isCheck[current] = false;
		dfs(current+1);	
	}
}

๐Ÿค” ๋‚˜์˜ ์ƒ๊ฐ

๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ํ•ฉ์œผ๋กœ ํ’€์–ด์ฃผ์—ˆ๋‹ค.
๊ฒฐ๊ตญ ํ•ฉ์ด S์ธ ๊ฒƒ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ๊ฐ์˜ ์›์†Œ๋ฅผ ๋‹ค ๋Œ์•„์ฃผ๋ฉด์„œ ๋”ํ•ด์„œ ํ•ฉ์ด S๊ฐ€ ๋˜๋ฉด ์นด์šดํŒ…์„ ํ•ด์ฃผ๊ณ  ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

ํƒœ๊ทธ: , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: