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

子集和找到所有加起來(lái)為一個(gè)數(shù)字的子集

subset sum find all subsets that add up to a number(子集和找到所有加起來(lái)為一個(gè)數(shù)字的子集)
本文介紹了子集和找到所有加起來(lái)為一個(gè)數(shù)字的子集的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我一直在學(xué)習(xí)動(dòng)態(tài)編程,我想通過(guò)打印所有加起來(lái)為一個(gè)數(shù)字的子集來(lái)進(jìn)一步解決經(jīng)典的子集求和問(wèn)題.我該怎么做呢?截至目前,我知道如何根據(jù)是否有一個(gè)子集相加來(lá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];
}

如果可能,我想返回一個(gè)包含所有子集的數(shù)組.

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

推薦答案

這個(gè)問(wèn)題比原來(lái)的問(wèn)題更難.

This problem is harder than the original one.

對(duì)于您設(shè)置為 true 的每個(gè) table[i][j],您必須標(biāo)記其所有 predecessors 即所有 table[i1][j1]=true,因此您將 table[i][j] 標(biāo)記為 true.通過(guò)這種方式,您可以構(gòu)建一種圖結(jié)構(gòu).該圖的頂點(diǎn)是對(duì)(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).

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

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.

這篇關(guān)于子集和找到所有加起來(lái)為一個(gè)數(shù)字的子集的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周?chē)h(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線(xiàn)程逐塊處理文件)
主站蜘蛛池模板: 久久亚洲欧美日韩精品专区 | 99国产视频| 最新日韩欧美 | 国产精品一区二区三区99 | 成人免费一区二区 | 精品国产乱码久久久久久影片 | 久久精品免费看 | 精品免费观看 | 欧美大片一区二区 | 亚洲日日 | 久久国色| 成人在线免费av | 天天插天天射天天干 | 久久久精品一区二区三区四季av | 亚洲精品高清视频在线观看 | 久久综合久久久 | 免费在线观看av的网站 | 国产精品美女久久久 | 中国黄色在线视频 | 91文字幕巨乱亚洲香蕉 | 狠狠撸在线视频 | 一区二区免费视频 | 亚洲黄色网址视频 | 中国一级特黄毛片大片 | 免费观看一级特黄欧美大片 | 久久久久久九九九九九九 | 在线观看视频一区二区三区 | 亚洲精品电影网在线观看 | 日本高清aⅴ毛片免费 | 天天操 天天操 | 一区视频在线播放 | 成人综合在线视频 | 亚洲日本一区二区 | 激情一区二区三区 | 国产激情一区二区三区 | 久久机热| 色综合九九 | 精品免费 | 欧美久久久久久久久中文字幕 | 伊人免费在线观看 | 天堂男人av|