ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/1182
package main
import (
"bufio"
"fmt"
"os"
)
var (
count int
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n, s int
fmt.Fscanln(reader, &n, &s)
var seq = make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscanf(reader, "%d ", &seq[i])
}
computeSubsequenceCount(0, 0, s, seq)
fmt.Fprintln(writer, count)
}
func computeSubsequenceCount(index, sum, aim int, seq []int) {
if index > len(seq)-1 {
return
}
sum += seq[index]
if sum == aim {
count++
}
computeSubsequenceCount(index+1, sum, aim, seq) // ํ์ฌ ์ธ๋ฑ์ค ๊ฐ์ ๋ํ์ง ์๋ ๊ฒฝ์ฐ
computeSubsequenceCount(index+1, sum-seq[index], aim, seq) // ํ์ฌ ์ธ๋ฑ์ค ๊ฐ์ ๋ํ๋ ๊ฒฝ์ฐ
}
๐จ Python3
# https://www.acmicpc.net/problem/1182
import sys
count = 0
seq = []
def compute_subsequence_count(index, sum, aim):
global count
if index > len(seq)-1:
return
sum += seq[index]
if sum == aim:
count += 1
compute_subsequence_count(index+1, sum, aim)
compute_subsequence_count(index+1, sum-seq[index], aim)
if __name__ == "__main__":
n, s = list(map(int, sys.stdin.readline().split()))
seq = list(map(int, sys.stdin.readline().split()))
compute_subsequence_count(0, 0, s)
print(count)
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1475๋ฒ / ๋ฐฉ ๋ฒํธ [Go][Python3] (0) | 2021.03.23 |
---|---|
BOJ / 6603๋ฒ / ๋ก๋ [Go][Python3] (0) | 2021.03.22 |
BOJ / 9465๋ฒ / ์คํฐ์ปค [Go][Python3] (0) | 2021.03.20 |
BOJ / 2294๋ฒ / ๋์ 2 [Go][Python3] (0) | 2021.03.19 |
BOJ / 11052๋ฒ / ์นด๋ ๊ตฌ๋งคํ๊ธฐ [Go][Python3] (0) | 2021.03.18 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ballet
- ๋งฅ๋ถํ๋ก
- ๋ฐฑ์ค
- ๋ชฝ๊ณ ๋๋น
- Algorithm
- ๋ถํ ์ ๋ณต
- ๋งฅ๋ถ
- ๋ฐ๋
- ์ด๋ถํ์
- BOJ
- ์๊ฐ๊ต์ฒด
- Golang
- ์๋ฐ
- ํ๋ก์ด๋์์ฌ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- MongoDB
- ์คํ
- BFS
- ์๊ณ ๋ฆฌ์ฆ
- ํด์๋งต
- go
- java
- Macbook pro 2012 mid 13
- dfs
- python3
- AWS
- ๋ธ๋ฃจํธํฌ์ค
- dp
- ํ
- baekjoon
- Total
- Today
- Yesterday