gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
⑧某年级的同学集体去公园划船,如果每只船坐10人,那么多出两个座位;如果每只船多坐两人,那么可少租1只船,设计一个算法用蛮力法求该年级的最多人数。 力扣中有相似的题目吗
- User
⑧某年级的同学集体去公园划船,如果每只船坐10人,那么多出两个座位;如果每只船多坐两人,那么可少租1只船,设计一个算法用蛮力法求该年级的最多人数。 力扣中有相似的题目吗
- Assistant
我们先来分析题目:
- 设船的数量为 ( x ),每只船正常坐 10 人。
- 如果每只船坐 10 人,总人数为 ( 10x ),但多出 2 个座位,说明实际人数为 ( 10x - 2 )。
- 如果每只船多坐 2 人,即每只船坐 12 人,只需要 ( x-1 ) 只船,总人数为 ( 12(x-1) )。
所以有方程: [ 10x - 2 = 12(x-1) ]
解这个方程: [ 10x - 2 = 12x - 12 ] [ 12x - 10x = 12 - 2 ] [ 2x = 10 ] [ x = 5 ]
代入 ( 10x - 2 ) 得到人数: [ 10 \times 5 - 2 = 48 ]
蛮力法算法设计
用蛮力法(暴力枚举)可以这样写伪代码:
max_people = 0 for x in range(1, 100): # 枚举船数 people1 = 10 * x - 2 people2 = 12 * (x - 1) if people1 == people2: max_people = people1 print(max_people)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.