더 많이 실패하기

공부 178일차: 백준 1182번 부분수열의 합 자바 java 본문

알고리즘/백준

공부 178일차: 백준 1182번 부분수열의 합 자바 java

김발자~ 2023. 1. 24. 20:45
반응형

1182 부분수열의 합

https://www.acmicpc.net/problem/1182

 

1182번: 부분수열의 합

첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.

www.acmicpc.net

 

 

 


백준 1182번 문제 부분수열의 합


문제


 

시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율
2 초 256 MB 61517 28343 18396 44.345%

문제

N개의 정수로 이루어진 수열이 있을 때, 크기가 양수인 부분수열 중에서 그 수열의 원소를 다 더한 값이 S가 되는 경우의 수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.

출력

첫째 줄에 합이 S가 되는 부분수열의 개수를 출력한다.

예제 입력 1

5 0
-7 -3 -2 5 8

예제 출력 1

1


 

 


과정 생각해보기 & 오답


 

오답부터..

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.io.*;
import java.util.*;
 
public class Main {
 
    static int n, s, length, count, sum;
    static int[] arr;
    
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        n = Integer.parseInt(st.nextToken());
        s = 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());
        }
        
        for(length = 1length < n+1length++) {
            DFS(00);
        }
        
        bw.write(count+"");
        bw.flush();
        bw.close();
    }
    
    static void DFS(int start, int depth) {
        if(depth == length) {
            if(sum == s) {                
                count++;
            }
            sum = 0;
            return;
        }
        
        for(int i = start; i < n; i++) {
            sum += arr[i];
//            System.out.println("sum : "+sum);
            DFS(start+1, depth+1);
        }
    }
 
}
// 시간 초과
cs

 

시작부분과 깊이를 달리 해서 모든 부분 수열을 보면 될 거라고 생각했는데 생각대로 구현되지 않았다

그런데 시간초과가 떠서 이 방향으로는 안 되겠다는 생각

 

그래서 이 블로그를 참고했다

 

 


정답 인정 코드


 

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
32
33
34
35
36
37
38
39
40
41
42
import java.io.*;
import java.util.*;
 
public class Main {
 
    static int n, s, count;
    static int[] arr;
    
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        n = Integer.parseInt(st.nextToken());
        s = 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());
        }
        
        if(s == 0) {
            count--;    // 아무것도 더하지 않은 0의 경우 제외
        }
        DFS(0,0);
        bw.write(count+"");
        bw.flush();
        bw.close();
    }
    
    static void DFS(int depth, int sum) {
        if(depth == n) {
            if(sum == s) {                
                count++;
            }
            return;
        }
        DFS(depth+1, sum+arr[depth]);
        DFS(depth+1, sum);
    }
}
cs

 

 

 


 

반응형
Comments