题目:“李白街上走,提壶去买酒,遇店加一倍,见花喝一斗”,途中,遇见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种
利用递归函数解决一道关于李白买酒见花问题的编程挑战,初始有2斗酒,途中遇见5次店、10次花,每次遇店酒量翻倍,见花喝一斗,最后酒要喝完且最后遇到的是花。代码实现中,通过递归遍历所有可能性,得出符合条件的情况共有14种。
610

被折叠的 条评论
为什么被折叠?



