You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于有限内置函数的递变资产购买量计算逻辑开发需求

Got it, let's break this down step by step. You need to implement two versions of a Buy() function—one for assets that increase in price by a fixed percentage per unit purchased, and another for assets that decrease in price by a fixed percentage per unit. Since you're working with a simple language with limited built-in math functions, we'll cover both an iterative approach (super compatible, no fancy math needed) and a formula-based approach (if your language has basic log/power functions).

First, let's define some shared variables we'll reference in both implementations:

  • base_price: The cost of the first asset unit
  • rate: The percentage change per unit (use increase_rate for price hikes, e.g., 0.05 for 5%; use discount_rate for price drops, e.g., 0.1 for 10%)

1. Buy() for Assets with Increasing Prices (Fixed % Per Unit)

Scenario:

Each subsequent asset costs (1 + increase_rate) times the previous one. First unit = base_price, second = base_price*(1+increase_rate), third = base_price*(1+increase_rate)^2, etc. We need to find the maximum number of units we can buy without exceeding the given budget.

Iterative Implementation (Best for Limited Math Functions)

This approach just accumulates costs until adding the next unit would go over budget—no need for logs or powers. Perfect for simple languages:

function Buy(budget):
    # Edge case: can't even afford the first unit
    if budget < base_price:
        return 0
    
    count = 0
    total_spent = 0
    current_unit_price = base_price
    increase_rate = 0.05  # Replace with your fixed percentage (e.g., 5% = 0.05)
    
    while total_spent + current_unit_price <= budget:
        total_spent += current_unit_price
        count += 1
        # Update price for next unit
        current_unit_price = current_unit_price * (1 + increase_rate)
    
    return count

Formula-Based Implementation (If Your Language Supports Logs/Powers)

We use the sum of a geometric series to calculate the maximum n directly. The sum of n increasing units is:
Total Cost = base_price * [(1 + increase_rate)^n - 1] / increase_rate

Solving for n gives us:
n ≤ log( (budget * increase_rate / base_price) + 1 ) / log(1 + increase_rate)

We still need to verify for floating-point errors:

function Buy(budget):
    if budget < base_price:
        return 0
    
    P0 = base_price
    r = 0.05  # Your fixed increase rate
    if r == 0:
        # No price change—just divide budget by base price
        return budget // P0  # Use integer division if available
    
    numerator = (budget * r) / P0 + 1
    if numerator <= 1:
        return 0
    
    # Calculate n using logarithms
    n = log(numerator) / log(1 + r)
    count = floor(n)  # Round down to integer
    
    # Verify to fix floating-point inaccuracies
    calculated_total = P0 * ((1 + r)**count - 1) / r
    if calculated_total > budget:
        count -= 1
    elif calculated_total + P0*(1 + r)**count <= budget:
        count += 1
    
    return count

2. Buy() for Assets with Decreasing Prices (Fixed % Per Unit)

Scenario:

Each subsequent asset costs (1 - discount_rate) times the previous one. First unit = base_price, second = base_price*(1-discount_rate), third = base_price*(1-discount_rate)^2, etc. Again, find the maximum units for the budget.

Iterative Implementation (Best for Limited Math Functions)

Similar to the increasing case, but we reduce the price each time. Note the edge case where a 100% discount makes all subsequent units free:

function Buy(budget):
    if budget < base_price:
        return 0
    
    count = 0
    total_spent = 0
    current_unit_price = base_price
    discount_rate = 0.1  # Replace with your fixed discount (e.g., 10% = 0.1)
    
    while total_spent + current_unit_price <= budget:
        total_spent += current_unit_price
        count += 1
        
        # Update price for next unit
        current_unit_price = current_unit_price * (1 - discount_rate)
        
        # Edge case: 100% discount means all remaining units are free
        if discount_rate == 1.0:
            # You can buy as many as you want (since they cost nothing)
            # Return a large integer if your language doesn't support "infinite"
            return count + 999999
    
    return count

Formula-Based Implementation (If Your Language Supports Logs/Powers)

The sum of n decreasing units is:
Total Cost = base_price * [1 - (1 - discount_rate)^n] / discount_rate

If the budget is large enough to cover the infinite sum (since prices approach 0), you can buy effectively unlimited units. Otherwise, solve for n:
n ≤ log(1 - (budget * discount_rate / base_price)) / log(1 - discount_rate)

Here's the code:

function Buy(budget):
    P0 = base_price
    d = 0.1  # Your fixed discount rate
    if budget < P0:
        return 0
    
    if d == 0:
        return budget // P0
    
    # Check if budget covers the infinite sum (prices approach 0)
    infinite_total = P0 / d
    if budget >= infinite_total:
        return 999999  # Replace with max integer for your language
    
    right_side = 1 - (budget * d) / P0
    if right_side <= 0:
        return 999999
    
    n = log(right_side) / log(1 - d)
    count = floor(n)
    
    # Verify for floating-point errors
    calculated_total = P0 * (1 - (1 - d)**count) / d
    if calculated_total > budget:
        count -= 1
    elif calculated_total + P0*(1 - d)**count <= budget:
        count += 1
    
    return count

Key Notes for Simple Languages:

  • If your language doesn't have floor(), use integer division (e.g., count = int(n) then adjust if needed)
  • The iterative method is almost always safer for limited environments—it avoids relying on advanced math functions and floating-point precision issues
  • Make sure to test edge cases: budget < base price, 0% rate (fixed price), 100% discount (if applicable)

内容的提问来源于stack exchange,提问作者Blue Blue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:33:49