題目:“李白街上走,提壺去買酒,遇店加一倍,見花喝一斗”,途中,遇見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種