ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
- BFS
- dp
- ํด์๋งต
- ballet
- Golang
- ์๊ณ ๋ฆฌ์ฆ
- java
- ํ
- baekjoon
- ๋ชฝ๊ณ ๋๋น
- dfs
- ๋ฐ๋
- AWS
- BOJ
- ์๋ฐ
- python3
- Algorithm
- ์๊ฐ๊ต์ฒด
- Macbook pro 2012 mid 13
- go
- ๋งฅ๋ถํ๋ก
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ฐฑ์ค
- ์คํ
- ์ด๋ถํ์
- ๋งฅ๋ถ
- ๋ถํ ์ ๋ณต
- ํ๋ก์ด๋์์ฌ
- MongoDB
- ๋ธ๋ฃจํธํฌ์ค
- Total
- Today
- Yesterday