Kattis平台Heart Rate问题:输入类型处理与浮点输出异常求助
Fixing the Output Format Issue in the Heart Rate Problem
Hey there! Let's break down what's going wrong with your code and how to fix it:
First, two key issues are causing your output to not match the problem's requirements:
- Incorrect data type for
p: Your original code converts bothbandpto integers usingmap(int, ...), but the problem statespis a real number (it can have decimal values). Converting it to an integer will truncate decimals and lead to wrong calculations. - Unformatted output: Even if your calculations are right, the default
print()statement doesn't enforce 4 decimal places. This means results like60.0will show up as60instead of60.0000, which fails the problem's formatting rules.
Here's the corrected code that addresses both issues:
n = int(input()) for i in range(n): # Split input first, then convert b to int and p to float separately b_str, p_str = input().split() b = int(b_str) p = float(p_str) bpm = 60 * b / p variance = 60 / p min_bpm = bpm - variance max_bpm = bpm + variance # Use f-strings to format each value to 4 decimal places print(f"{min_bpm:.4f} {bpm:.4f} {max_bpm:.4f}")
Let's walk through the changes:
- Data type correction: We split the input into two strings first, then convert
bto integer (as required) andpto float to preserve any decimal values. This ensures your calculations use the correct real number values forp. - Output formatting: The
:.4fin the f-string tells Python to format each floating-point number to exactly 4 decimal places. Even if the result is a whole number (like30.0), it will be printed as30.0000to meet the problem's output requirements.
For example, if you input:
1 5 10
The corrected code will output:
24.0000 30.0000 36.0000
Which matches exactly what the problem expects.
内容的提问来源于stack exchange,提问作者user11106339
相关产品推荐
相关产品推荐

