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

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:

  1. Incorrect data type for p: Your original code converts both b and p to integers using map(int, ...), but the problem states p is a real number (it can have decimal values). Converting it to an integer will truncate decimals and lead to wrong calculations.
  2. Unformatted output: Even if your calculations are right, the default print() statement doesn't enforce 4 decimal places. This means results like 60.0 will show up as 60 instead of 60.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 b to integer (as required) and p to float to preserve any decimal values. This ensures your calculations use the correct real number values for p.
  • Output formatting: The :.4f in the f-string tells Python to format each floating-point number to exactly 4 decimal places. Even if the result is a whole number (like 30.0), it will be printed as 30.0000 to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 12:22:38