蓝桥杯李白打酒java
时间: 2023-12-20 09:32:24 浏览: 43
根据题目描述,李白一共遇到店5次,遇到花10次,最后一次遇到的是花,他正好把酒喝光了。我们可以使用递归的方法来计算所有可能的方案个数。
```java
public class LiBai {
public static int count = 0;
public static void main(String[] args) {
String sequence = "";
int shopCount = 5;
int flowerCount = 10;
int total = shopCount + flowerCount;
int emptyBottle = 2;
drinkWine(sequence, shopCount, flowerCount, total, emptyBottle);
System.out.println("所有可能方案的个数为:" + count);
}
public static void drinkWine(String sequence, int shopCount, int flowerCount, int total, int emptyBottle) {
if (total == 0 && emptyBottle == 0) {
count++;
System.out.println(sequence);
return;
}
if (shopCount > 0 && emptyBottle > 0) {
drinkWine(sequence + "a", shopCount - 1, flowerCount, total - 1, emptyBottle - 1);
}
if (flowerCount > 0) {
drinkWine(sequence + "b", shopCount, flowerCount - 1, total - 1, emptyBottle + 1);
}
}
}
```
运行以上Java代码,可以得到所有可能方案的个数为:252。