久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

子集和找到所有加起來為一個數字的子集

subset sum find all subsets that add up to a number(子集和找到所有加起來為一個數字的子集)
本文介紹了子集和找到所有加起來為一個數字的子集的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我一直在學習動態(tài)編程,我想通過打印所有加起來為一個數字的子集來進一步解決經典的子集求和問題.我該怎么做呢?截至目前,我知道如何根據是否有一個子集相加來打印真假

I have been learning dynamic programming, and I want to take the classic subset sum problem a little further by printing out all the subsets which add up to a number. How exactly would I go about doing this? As of now, I know how to print true or false based on whether there is a subset that adds up to it

    public static boolean hasSum(int [] array, int sum)
{
    int len = array.length;
    boolean[][] table = new boolean[sum+1][len+1];

    int i;

    for( i = 0; i <= len; i++ )
        table[0][i] = true;

    for( i = 1; i <= sum; i++ )
        table[i][0] = false;

    for( i = 1; i <= sum; i++ )
    {
        for( int j = 1; j <= len; j++ )
        {
            table[i][j] = table[i][j-1]; 

            if( !table[i][j] && i >= array[j-1] )
                table[i][j] = table[i-array[j-1]][j-1];
        }
    }        

    return table[sum][len];
}

如果可能,我想返回一個包含所有子集的數組.

if possible, I'd like to return an array of all of the subsets.

推薦答案

這個問題比原來的問題更難.

This problem is harder than the original one.

對于您設置為 true 的每個 table[i][j],您必須標記其所有 predecessors 即所有 table[i1][j1]=true,因此您將 table[i][j] 標記為 true.通過這種方式,您可以構建一種圖結構.該圖的頂點是對(i,j).

For each table[i][j] which you set to true, you have to mark all its predecessors i.e. all the table[i1][j1]=true, due to which you marked table[i][j] as true. This way you build a kind of graph structure. The vertices of this graph are couples (i,j).

然后當你想打印答案時,你必須從 (i,j) 回溯到所有可能的 (i1,j1) 等等.為此,僅一個數組是不夠的,您需要額外的/輔助數據結構.

Then when you want to print the answer, you have to trace back from (i,j) to all possible (i1,j1) and so on going backwards. For this, just an array won't be enough, you'll need additional/helper data structures.

這篇關于子集和找到所有加起來為一個數字的子集的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 九九爱这里只有精品 | 日韩精品一区二区三区中文字幕 | 国产精品久久久久久久三级 | 一区二区日本 | 亚洲乱码一区二区三区在线观看 | 日本精品一区二区三区视频 | 91精品福利 | 免费精品 | 欧美日韩国产一区二区三区 | 国产成人影院 | 青青草中文字幕 | 中文字幕视频在线 | 久久av网| 久久久久久91 | www.久久99 | 亚洲精品91 | av中文字幕在线 | 亚洲欧美日韩国产 | 国产日韩欧美一区二区在线播放 | 欧美日韩高清在线一区 | 黑人粗黑大躁护士 | 午夜网| 色av一区二区 | 欧美一区二区在线视频 | 日韩免费成人av | 黄视频网站在线 | 国产毛片久久久久久久久春天 | 91精品免费 | 超碰在线播 | 国产精品夜间视频香蕉 | 免费一级片| 久久精品视频在线观看 | 欧美日韩精品一区二区三区视频 | a在线免费观看视频 | 日韩欧美专区 | 国产高清免费 | hitomi一区二区三区精品 | 欧美精品1区 | 一区二区三区四区av | 久久久av | 婷婷中文字幕 |