Hướng dẫn python subset sum - Tôi là một tập hợp con của trăn

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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 sum
    
    0
    Found a subset with given sum
    
    1sum
    Found a subset with given sum
    
    3
    Found a subset with given sum
    
    3
    Found a subset with given sum
    
    5) :

    Found a subset with given sum
    
    7
    Found a subset with given sum
    
    8
    Found a subset with given sum
    
    9

        

    Found a subset with given sum
    
    0

    Found a subset with given sum
    
    7
    Found a subset with given sum
    
    8 def4

        

    Found a subset with given sum
    
    0
    Found a subset with given sum
    
    1setdef9isSubsetSum(0 isSubsetSum(1isSubsetSum(2sum) :

    Found a subset with given sum
    
    7
    Found a subset with given sum
    
    8 isSubsetSum(setisSubsetSum(9isSubsetSum(0 isSubsetSum(1set2____________set4

        ______18

    Is

    sum

    Found a subset with given sum
    
    3     6

        

    Found a subset with given sum
    
    14
    Found a subset with given sum
    
    1
    Found a subset with given sum
    
    16
    Found a subset with given sum
    
    02

        7

    Found a subset with given sum
    
    3     9
    Found a subset with given sum
    
    1set
    Found a subset with given sum
    
    02

        

    Found a subset with given sum
    
    14
    Found a subset with given sum
    
    1
    Found a subset with given sum
    
    23
    Found a subset with given sum
    
    02

    Output:

    Found a subset with given sum
    

    Is

    Python3

    Found a subset with given sum
    
    18
    Found a subset with given sum
    
    19

    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 sum
    
    30

        

    Found a subset with given sum
    
    32
    Found a subset with given sum
    
    3
    Found a subset with given sum
    
    34def4
    Found a subset with given sum
    
    36
    Found a subset with given sum
    
    37__

    Found a subset with given sum
    
    45
    Found a subset with given sum
    
    36
    Found a subset with given sum
    
    37
    Found a subset with given sum
    
    38
    Found a subset with given sum
    
    39
    Found a subset with given sum
    
    22

        

    Found a subset with given sum
    
    36
    Found a subset with given sum
    
    37
    Found a subset with given sum
    
    38
    Found a subset with given sum
    
    39
    Found a subset with given sum
    
    22

    Found a subset with given sum
    
    7
    Found a subset with given sum
    
    64
    Found a subset with given sum
    
    5
    Found a subset with given sum
    
    66
    Found a subset with given sum
    
    3
    Found a subset with given sum
    
    9

    Found a subset with given sum
    
    7
    Found a subset with given sum
    
    36
    Found a subset with given sum
    
    37
    Found a subset with given sum
    
    38
    Found a subset with given sum
    
    39
    Found a subset with given sum
    
    1____41set22

    Found a subset with given sum
    
    45
    Found a subset with given sum
    
    82
    Found a subset with given sum
    
    5
    Found a subset with given sum
    
    84
    Found a subset with given sum
    
    3 def4

    Found a subset with given sum
    
    7
    Found a subset with given sum
    
    36
    Found a subset with given sum
    
    37
    Found a subset with given sum
    
    38
    Found a subset with given sum
    
    39
    Found a subset with given sum
    
    1____41__

    Found a subset with given sum
    
    45
    Found a subset with given sum
    
    36
    Found a subset with given sum
    
    00
    Found a subset with given sum
    
    38

    Các

    Found a subset with given sum
    
    18
    Found a subset with given sum
    
    19
    Found a subset with given sum
    
    3
    Found a subset with given sum
    
    21isSubsetSum(0isSubsetSum(1
    Found a subset with given sum
    
    24

    Found a subset with given sum
    
    10
    Found a subset with given sum
    
    0
    Found a subset with given sum
    
    27
    Found a subset with given sum
    
    3 set
    Found a subset with given sum
    
    14isSubsetSum(0isSubsetSum(1
    Found a subset with given sum
    
    17

    Found a subset with given sum
    
    18
    Found a subset with given sum
    
    19
    Found a subset with given sum
    
    3 ________ 237 ________ 40 ________ 41 ________ 240 ________ 65 & nbsp;

    Found a subset with given sum
    
    42
    Found a subset with given sum
    
    43isSubsetSum(0 isSubsetSum(1
    Found a subset with given sum
    
    46__404055214isSubsetSum(0isSubsetSum(1
    Found a subset with given sum
    
    52

        

    Found a subset with given sum
    
    8
    Found a subset with given sum
    
    55sum__

    Found a subset with given sum
    
    0
    Found a subset with given sum
    
    59
    Found a subset with given sum
    
    3
    Found a subset with given sum
    
    3
    Found a subset with given sum
    
    62 ____

        set

    Found a subset with given sum
    
    3 ) :1) :2set2) :4set2) :6set22.

        sum

    Found a subset with given sum
    
    3     6

    Found a subset with given sum
    
    7
    Found a subset with given sum
    
    14
    Found a subset with given sum
    
    1
    Found a subset with given sum
    
    16
    Found a subset with given sum
    
    02

        

    Found a subset with given sum
    
    18
    Found a subset with given sum
    
    19

    Found a subset with given sum
    
    7
    Found a subset with given sum
    
    14
    Found a subset with given sum
    
    1
    Found a subset with given sum
    
    23
    Found a subset with given sum
    
    02

    Output:

    Found a subset with given sum
    

            7

    Found a subset with given sum
    
    3     9
    Found a subset with given sum
    
    1set
    Found a subset with given sum
    
    02