java應用簡單遞迴

來源:互聯網
上載者:User

標籤:怎麼學   個數   images   組成   重要   code   system   collect   png   

畢業後就怎麼學過演算法,還在上學的時候學過資料結構,現在基本上都還給老師了,可惜老師學費沒有還給我。。。

情景: 類似於給定一個數字,算他由多少個數字組成,比如:36 現在有10、5、1 ,那麼最佳帥3個10,1個5,1個1組成(預設優先取最大)。因為我們這邊業務上面需要開發票,有的時候不想一個一個算,就讓我們寫一個。但是他發票的個數不是固定的、無盡的,所以每次就要判斷下,如果:36 現在基數還是10、5、1 但是 只讓你拿1個10、2個5、若干個1,那麼組合應該是:1個10,2個5,16個1,大概就是這個意思。

添加兩個list,一個存放基數(從大到小)另外一個存放基數的對應個數。

     List list = new ArrayList();        list.add(10);        list.add(100);        list.add(1);        list.add(20);        list.add(50);        list.add(5);        Collections.sort(list, Collections.reverseOrder());        List list2 = new ArrayList();         list2.add(1);  //100對應個數(下同)        list2.add(4); //50        list2.add(1); //20        list2.add(20); //10        list2.add(50); //5        list2.add(1000); //1

下面就是遞迴了。。沒有啥難度。。參數分別是: 輸入的總數,集合的初始下標,基數集合,基數個數集合

public void c(int totle, int flag, List list, List list2) {        //System.out.println("當前總額" + totle + ",當前額度" + list.get(flag));        int sum = 0;        for (int i = 0; i < list.size(); i++) {            sum += (int) list.get(i) * (int) list2.get(i);        }        if (sum < totle) {            System.out.println("發票總額不夠,請添加發票!!!!");        } else {            if (totle % (int) list.get(flag) == 0) {                int s = totle / (int) list.get(flag);                //System.out.println(s + "");                if (s > (int) list2.get(flag)) {                    System.out.println("需要" + list.get(flag) + "元的發票" + s + "張,目前只有" + list2.get(flag) + "張,全部使用!");                    totle -= (int) list2.get(flag) * (int) list.get(flag);                    System.out.println(totle);                    c(totle, ++flag, list, list2);                } else {                    System.out.println("需要" + list.get(flag) + "元的發票" + s + "張");                }            } else {                int s = totle / (int) list.get(flag);                if (s > (int) list2.get(flag)) {                    System.out.println("需要" + list.get(flag) + "元的發票" + s + "張,目前只有" + list2.get(flag) + "張,全部使用!");                    totle -= (int) list2.get(flag) * (int) list.get(flag);                    System.out.println(totle);                    c(totle, ++flag, list, list2);                } else {                    totle = totle % (int) list.get(flag);                    System.out.println("需要" + list.get(flag) + "元的發票" + s + "張");                    System.out.println(totle);                    c(totle, ++flag, list, list2);                }            }        }    }

假設:

c(365, 0, list, list2);

輸出

如果輸入超過所有的總額,那麼如下:

 

很簡單,記錄一下,也是提醒下自己,演算法也是蠻重要的。。。沒事也要跟著慢慢研究!!! 

 

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.