Li Bai's drinking problem
Title description
Li Bai hits the wine
- One day Li Bai came out with wine. There were 2 buckets of wine in the jug. He sang as he walked:
- Walking on the street without incident, picking up a pot to drink, doubling every time in the store, and shaking when encountering flowers.
- I met the store 5 times, spent 10 times, and finally met the flower, just drank the wine
- Calculate possible solutions
Idea analysis
Two buckets of wine, I saw the wine in the store*2, I saw that the wine was spent -1, and finally I saw the flower just finished drinking.
I saw 5 times in the shop and 10 times to spend.
Calculate the number of plans.
This question is a typical search problem. The
ending condition is After reading the shop, there is one flower left and one bucket of wine left. The
problem is simplified to 5 shops, 10 flowers, and finally one bucket of wine and one flower.
code show as below
private static int count;
private static void countresult(int store,int flowers,int wine) {
if(store==0&&flowers==1&&wine==1) count++;
if(store>0)countresult(store-1,flowers,wine*2);
if(flowers>0)countresult(store,flowers-1,wine-1);
}
public static void main(String[] args) {
countresult(5,10,2);
System.out.println(count);
}
Pit position reminder When
I did it for the first time, I set the initial value of flowers to 9, and the initial value of wine to 1, and the judgment was changed to wine=0, flowers=0; the
addition and subtraction of flowers will not affect the result, and Wine is a multiplication operation, and the multiplication operation will affect the final result.
Finally got the result 14