JAVA函數實現任意給定一組數, 找出任意數相加之後的結果為35

來源:互聯網
上載者:User

用JAVA寫一個函數。功能如下:任意給定一組數,例如{12,60,-8,99,15,35,17,18},找出任意數相加之後的結果為35(任意設定)的情況.

可以遞迴演算法來解:

package test1;    import java.util.Arrays;    public class demo {            public static void main(String[] args) {        String str = "12,60,-8,99,15,35,17,18,8,10,11,12";        int sum = 35;        diguiSum(str,sum);    }            public static void diguiSum(String str,int sum) {        String[] x = str.split(",");        int[] array = arrayTransform(x);        for (int i = 0; i < array.length; i++) {            int[] cache = new int[i + 1];            int ceng = -1;            int cengQuit = i;            int startPiont = 0;            cir(ceng, cengQuit, startPiont, array, cache, sum);        }    }        // 遞迴求結果    public static void cir(int ceng, int cengQuit, int startPiont, int[] array, int[] cache, int sum) {        ceng++;        for (int i = startPiont; i < array.length; i++) {            cache[ceng] = array[i];            if (ceng == cengQuit) {                if (getSum(cache) == sum) {                    printcache(cache);                }                if (getSum(cache) > sum) {                    break;                }            }            if (ceng < cengQuit) {                startPiont = i + 1;                cir(ceng, cengQuit, startPiont, array, cache,sum);            }        }    }        // 擷取組合數字之和    public static int getSum(int[] cache) {        int sum = 0;        for (int i = 0; i < cache.length; i++) {            sum = sum + cache[i];        }        return sum;    }        // 列印組合的可能    public static void printcache(int[] cache) {        for (int i = 0; i < cache.length; i++) {            System.out.print(cache[i] + ",");        }        System.out.println();    }        // 轉換數群組類型 且為提高效率做準備    public static int[] arrayTransform(String[] strArray) {        int length = 0;            int[] array = new int[strArray.length];        for (int i = 0; i < strArray.length; i++) {            array[i] = Integer.valueOf(strArray[i]);        }        Arrays.sort(array);        for (int i = 0; i < array.length; i++) {            if (array[i] > 35) {                length = i;                break;            }        }        int[] dest = new int[length];        System.arraycopy(array, 0, dest, 0, length);        return dest;    }}

此演算法有做適當參考。

運行結果:

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.