藍橋杯李白醉酒問題

題目:“李白街上走,提壺去買酒,遇店加一倍,見花喝一斗”,途中,遇見5次店,見了10此花,壺中原有2斗酒,最後剛好喝完酒,要求最後遇見的是花,求可能的情況有多少種?

分析:這道題可通過遞歸函數來實現。

#include <stdio.h>


int count = 0;
int libai (int store, int flower, int alco, int pre)
{
if (store == 0 && flower == 0)
{
if (alco == 0 && pre == 0)
count++;
return;
}


if (store > 0)
libai (store-1, flower, alco*2, 1);
if (flower > 0)
   libai (store, flower-1, alco-1, 0);

}
int main()
{
libai (5, 10, 2, -1);
printf ("%d",count);
    return 0;
}

最後結果爲14種

發佈了31 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章