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 |
Tags
- 알고리즘
- lambda
- kotlin
- algorithm
- java
- back-end
- 운영체제
- TDD
- Java8
- DFS
- OS
- BFS
- programmers
- 백트래킹
- Spring
- LEVEL2
- 모던자바
- 자료구조
- 네트워크
- 백준
- 자바
- backtracking
- 프로젝트
- baekjoon
- 그래프
- 프로그래머스
- 코틀린
- DP
- 스프링
- Brute-force
Archives
- Today
- Total
요깨비's LAB
[백준, Brute-Force, Java] P.1182 부분수열의 합 본문
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int N, S;
static int count = 0;
static int[] arr;
public static void main(String[] args) {
YoggaebReader scr = new YoggaebReader();
N = scr.nextInt();
S = scr.nextInt();
arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = scr.nextInt();
}
for (int i = 0; i < N; i++) {
doAlgorithm(i, arr[i]);
}
System.out.print(count);
}
public static void doAlgorithm(int idx, int value) {
if (idx == N) {
return;
}
if (value == S) {
count++;
}
for (int i = idx + 1; i < N; i++) {
doAlgorithm(i, value + arr[i]);
}
}
static class YoggaebReader {
BufferedReader br;
StringTokenizer st;
public YoggaebReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public YoggaebReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
'알고리즘(Java) > Brute-Force' 카테고리의 다른 글
[백준, Brute-Force, Java] P.2470 두 용액 (0) | 2021.10.14 |
---|---|
[백준, Brute-Force, Java] P.7795 먹을 것인가 먹힐 것인가 (0) | 2021.10.02 |
[백준, Brute-Force, Java] P.9663 n-queen (0) | 2021.09.30 |
Comments