This is a fun programming puzzle about peaches (桃子) and peach pits (桃核). The rules are as follows:

  • You start with some amount of money and some peach pits.
  • Each turn, you can use 1 元 to buy one peach, or exchange 3 peach pits for one peach.
  • After you eat one peach, you gain 1 peach pit.

The final goal is: eat as many peaches as possible. Below is the complete solution idea and the code.

Solution Idea

The key to this problem is to make full use of the value of the peach pits and avoid waste. Do the following steps:

  1. At each step, convert the value of the peach pits into money to maximize your available funds.
  2. If your money is enough, buy one peach and eat it right away.
  3. When you can no longer keep buying, check whether you can “exchange once more” to ensure the best possible use of resources.

Program Code

Here is the complete code implemented in Python:

money = 10
peach_pits = 0
peaches_eaten = 0

while True:
    money += peach_pits / 3
    peach_pits = 0
    
    if money < 1:
        if money + 1/3 >= 1:
            money += 1/3
            peach_pits -= 1
        else:
            break
    
    money -= 1
    peaches_eaten += 1
    peach_pits += 1

print(f"总共吃了 {peaches_eaten} 个桃子")