Elementary Math Problems and Branching Logic in Python
import math
def solve_n_in_one(problem_id):
"""A dispatcher for multiple elementary math and reasoning problems."""
solutions = {
1: lambda: print('I love Luogu!'),
2: lambda: print(6, 4), # A + Uim, remaining for B
3: lambda: (print(3), print(12), print(2)), # each, given out, returned
4: lambda: print(f"{500 / 3:.6f}"),
5: lambda: print((260 + 220) // (12 + 20)),
6: lambda: print(f"{math.hypot(6, 9):.4f}"),
7: lambda: (print(110), print(90), print(0)),
8: lambda: print_geometry(),
9: lambda: print(calculate_peaches()),
10: lambda: print(solve_evaluation_machines()),
11: lambda: print(f"{100 / (8 - 5):.4f}"),
12: lambda: print_letters(),
13: lambda: print(cube_edge()),
14: lambda: print(optimal_pricing())
}
problem = solutions.get(problem_id)
if problem:
problem()
def print_geometry():
pi = 3.141593
r = 5
circumference = f"{2 * pi * r:.4f}"
area = f"{pi * r * r:.4f}"
volume = f"{(4 / 3) * pi * r ** 3:.3f}"
for value in (circumference, area, volume):
print(value)
def calculate_peaches():
remaining = 1
for _ in range(3):
remaining = (remaining + 1) * 2
return remaining
def solve_evaluation_machines():
# Using the classic "cows eating grass" variant equation:
# Initial queue + rate * time = machines * time
# 8 * 30 = I + 30v, 10 * 6 = I + 6v
rate = (8 * 30 - 10 * 6) / (30 - 6) # growth rate
initial = 10 * 6 - rate * 6
needed = int((initial + rate * 10) / 10)
return needed
def print_letters():
m_position = ord('M') - ord('A') + 1
eighteenth = chr(ord('A') + 17)
print(m_position, eighteenth, sep='\n')
def cube_edge():
pi = 3.141593
radii = [4, 10]
combined_volume = sum((4 / 3) * pi * r ** 3 for r in radii)
edge = combined_volume ** (1 / 3)
return int(edge)
def optimal_pricing():
price = 110
enrollees = 10
candidates = []
while price > 0:
revenue = price * enrollees
if revenue >= 3500:
# If exact target possible, record the price; stop if revenue goes below 3500 later?
# The original logic collected ALL prices that yield exactly 3500.
if revenue == 3500:
candidates.append(price)
else:
break
price -= 1
enrollees += 1
return min(candidates) if candidates else round(3500 / enrollees_estimate())
# Note on Problem 14: original problem expects two answers; take the smaller one after rounding if needed.
# Extended logic ensures minimal price that meets or exceeds 3500, but adjust per statement.
# Problem 10 (machines) can also be derived from linear equations.
Branching Structure with Aples
A situation involving consuming apples over time requires careful condition checks. Consider m apples and a per-apple eating time t. The goal is to determine how many comlpete apples remain after a given duration s.
def remaining_apples(m, t, s):
"""
Return the count of whole apples left after s time units,
assuming m starting apples and t time units to finish one apple.
Several edge cases exist: t = 0, s exceeding total time, partial bites, etc.
"""
if t == 0:
# Instant consumption – all apples gone immediately
return 0
total_time_needed = m * t
if s >= total_time_needed:
return 0
consumed_completely = s // t
# If a partial apple is being eaten at the exact moment, it is not counted as remaining.
partial = 1 if s % t != 0 else 0
eaten = consumed_completely + partial
remaining = m - eaten
return max(0, remaining)
# Example usage:
# m, t, s = 10, 2, 5 -> 7 apples left
This logic avoids floating-point inaccuracies by using integer division and modulo. When t is zero, all apples vanish instantly. If the elapsed time s covers the total eating duration exactly, no apples remain.