POJ 放蘋果問題(遞迴)

來源:互聯網
上載者:User

標籤:兩種   題目   one   end   namespace   就是   using   lis   lse   

首先我們想象有一個函數count(m,n)可以把m個蘋果放到n個盤子中。

 

根據 n 和 m 的關係可以進一步分析:

  特殊的m <=1|| n <= 1時只有一種方法;

  當 m < n時,即使蘋果每個盤子放一個也沒法放滿所有盤子,題目允許有的盤子空著不放,所以我們可以將空盤子去掉,即 count ( m , n ) = count ( m , m );

  當 m >= n時,這時候有兩種情況:

  n 個盤子中有一個空盤子,當有空盤子時,count ( m , n ) = count ( m , n - 1 );

  n個盤子中沒有空盤子,當沒有空盤子時也就是說每個盤子中至少有一個蘋果,先把所有盤子填滿,這時候會剩下 m - n 個蘋果,所以現在問題變成了 m - n 個蘋果放在 n 個盤子有多少種方法,即 count ( m - n , n )。

      所以當m>=n時,放置蘋果的總情況為 count ( m , n - 1 )+ count ( m - n , n )次。

  具體代碼實現如下:

#include <iostream>using namespace std;int count(int m, int n){    if (m <=1|| n <= 1)         return 1;    if (m < n)        return count(m, m);    else        return count(m, n - 1) + count(m - n, n);}int main(){    int m, n;    cin >> m >> n;        cout << count(m, n) << endl;    return 0;}

 

 

POJ 放蘋果問題(遞迴)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.