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

Python计算分组教室最小课桌数代码错误排查及修正咨询

Fixing the Desk Calculation Logic for Independent Groups

Let's break down why your code isn't passing all test cases, and how to fix it.

The Root Problem

Your current approach calculates the total number of students across all three groups, divides by 2, and rounds up. But the problem explicitly states each group has independent classrooms and desks—you can't mix students from different groups at the same desk! That means you need to calculate the required desks for each group individually, then add those numbers together.

For example, take the failing test case with inputs 17, 22, 23:

  • Your code calculates total students = 62, divides by 2 to get 31, which rounds up to 31. But this is wrong:
    • Group 1 (17 students): Needs 9 desks (8 desks for 16 students, 1 extra for the remaining 1)
    • Group 2 (22 students): Needs 11 desks (exactly 2 per desk)
    • Group 3 (23 students): Needs 12 desks (11 desks for 22 students, 1 extra for the last one)
  • Total correct desks: 9 + 11 + 12 = 32, not 31.

Correct Code Implementations

You have two solid options to calculate the desks per group:

Option 1: Using math.ceil()

This is straightforward—just apply the ceiling function to each group's student count divided by 2:

import math

# Read input values (abs handles any accidental negative inputs, as you already did)
grp1 = abs(int(input()))
grp2 = abs(int(input()))
grp3 = abs(int(input()))

# Calculate desks for each group individually
desk_grp1 = math.ceil(grp1 / 2)
desk_grp2 = math.ceil(grp2 / 2)
desk_grp3 = math.ceil(grp3 / 2)

# Sum to get total required desks
total_desks = desk_grp1 + desk_grp2 + desk_grp3
print(total_desks)

Option 2: Integer Arithmetic (No math Module Needed)

If you want to avoid floating-point operations, you can use integer division with a small adjustment. (n + 1) // 2 is a common trick to get the ceiling of n/2 using only integers:

grp1 = abs(int(input()))
grp2 = abs(int(input()))
grp3 = abs(int(input()))

# Integer-based ceiling division for each group
desk_grp1 = (grp1 + 1) // 2
desk_grp2 = (grp2 + 1) // 2
desk_grp3 = (grp3 + 1) // 2

total_desks = desk_grp1 + desk_grp2 + desk_grp3
print(total_desks)

Verify with Your Test Cases

  • For input 20 21 22:
    • 20 → 10 desks, 21 → 11 desks, 22 → 11 desks
    • Total: 10 + 11 + 11 = 32 (matches the expected output)
  • For input 17 22 23:
    • Total: 9 + 11 + 12 = 32 (fixes the failing test case)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 11:07:34