Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Example:
Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9 Output: True //There is a subset [4, 5] with sum 9.
Bàn luậnPRACTICE ” first, before moving on to the solution.
Đưa ra một tập hợp các số nguyên không âm và tổng giá trị, xác định xem có một tập hợp con của tập hợp đã cho với tổng bằng với tổng.extrong.example:
Python3
Được đề xuất: Vui lòng giải quyết nó trên thực hành trực tuyến trước, trước khi chuyển sang giải pháp.
Sau đây là việc thực hiện đệ quy ngây thơ chỉ đơn giản là theo cấu trúc đệ quy được đề cập ở trên.
def
isSubsetSum[
set
, n,
sum
Found a subset with given sum0
Found a subset with given sum1
sum
Found a subset with given sum3
Found a subset with given sum3
Found a subset with given sum5
] :
Found a subset with given sum7
Found a subset with given sum8
Found a subset with given sum9
Found a subset with given sum0
Found a subset with given sum7
Found a subset with given sum8
def
4
Found a subset with given sum0
Found a subset with given sum1
set
def
9isSubsetSum[
0 isSubsetSum[
1isSubsetSum[
2sum
] :
Found a subset with given sum7
Found a subset with given sum8
isSubsetSum[
set
isSubsetSum[
9isSubsetSum[
0 isSubsetSum[
1set
2____________set
4
______18
Is
sum
Found a subset with given sum3
6
Found a subset with given sum14
Found a subset with given sum1
Found a subset with given sum16
Found a subset with given sum02
7
Found a subset with given sum3
9Found a subset with given sum1
set
Found a subset with given sum02
Found a subset with given sum14
Found a subset with given sum1
Found a subset with given sum23
Found a subset with given sum02
Output:
Found a subset with given sum
Is
Python3
Found a subset with given sum18
Found a subset with given sum19
Chúng ta có thể giải quyết vấn đề trong thời gian giả-polynomial bằng cách sử dụng lập trình động.
def
isSubsetSum[
set
, n,
sum
Found a subset with given sum30
Found a subset with given sum32
Found a subset with given sum3
Found a subset with given sum34
def
4 Found a subset with given sum36
Found a subset with given sum37__
Found a subset with given sum45
Found a subset with given sum36
Found a subset with given sum37
Found a subset with given sum38
Found a subset with given sum39
Found a subset with given sum22
Found a subset with given sum36
Found a subset with given sum37
Found a subset with given sum38
Found a subset with given sum39
Found a subset with given sum22
Found a subset with given sum7
Found a subset with given sum64
Found a subset with given sum5
Found a subset with given sum66
Found a subset with given sum3
Found a subset with given sum9
Found a subset with given sum7
Found a subset with given sum36
Found a subset with given sum37
Found a subset with given sum38
Found a subset with given sum39
Found a subset with given sum1____41
set
22Found a subset with given sum45
Found a subset with given sum82
Found a subset with given sum5
Found a subset with given sum84
Found a subset with given sum3
def
4Found a subset with given sum7
Found a subset with given sum36
Found a subset with given sum37
Found a subset with given sum38
Found a subset with given sum39
Found a subset with given sum1____41__
Found a subset with given sum45
Found a subset with given sum36
Found a subset with given sum00
Found a subset with given sum38
Các
Found a subset with given sum18
Found a subset with given sum19
Found a subset with given sum3
Found a subset with given sum21
isSubsetSum[
0isSubsetSum[
1Found a subset with given sum24
Found a subset with given sum10
Found a subset with given sum0
Found a subset with given sum27
Found a subset with given sum3
set
Found a subset with given sum14
isSubsetSum[
0isSubsetSum[
1Found a subset with given sum17
Found a subset with given sum18
Found a subset with given sum19
Found a subset with given sum3 ________ 237 ________ 40 ________ 41 ________ 240 ________ 65 & nbsp;
Found a subset with given sum42
Found a subset with given sum43
isSubsetSum[
0 isSubsetSum[
1Found a subset with given sum46__404055214
isSubsetSum[
0isSubsetSum[
1Found a subset with given sum52
Found a subset with given sum8
Found a subset with given sum55
sum
__Found a subset with given sum0
Found a subset with given sum59
Found a subset with given sum3
Found a subset with given sum3
Found a subset with given sum62 ____
set
Found a subset with given sum3
] :
1] :
2set
2] :
4set
2] :
6set
22.
sum
Found a subset with given sum3
6Found a subset with given sum7
Found a subset with given sum14
Found a subset with given sum1
Found a subset with given sum16
Found a subset with given sum02
Found a subset with given sum18
Found a subset with given sum19
Found a subset with given sum7
Found a subset with given sum14
Found a subset with given sum1
Found a subset with given sum23
Found a subset with given sum02
Output:
Found a subset with given sum
7
Found a subset with given sum3
9Found a subset with given sum1
set
Found a subset with given sum02