解题思路:检查每一小时内有几位名人在场,并选出最大值,该时刻就是参加派对的最佳时间。
名人 | 出席时间 | 离开时间 |
名人1 | 6 | 7 |
名人2 | 6 | 8 |
名人3 | 6 | 12 |
名人4 | 7 | 8 |
名人5 | 7 | 10 |
5
6
7
6
8
6
12
7
8
7
10
最佳出席时间是在7点钟;4个名人会在现场!
输入名人数量、参加和离开派对时间及输出结果
def bestTimeToParty(schedule):
start = schedule[θ][θ]
end = schedule[θ][1]
for c in schedule:
start = min(c[θ],start)
end=max(c[1],end)
①
maxcount=θ
for i in range(start,end+1):
if count[i]>maxcount:
maxcount = count[i]
time =i
print('最佳出席时间是在',time,'点钟',':',maxcount,'个名人会在现场!')
def statistics(sched,start,end): #寻找每个时间段的现场人数
count=[θ]*end |
for i in range(start, end+1):
count[i]= θ
for c in sched:
if ②
count[i]+=1
return count
n=int(input()) #输入参加派对的名人数量
sched= [ ] #保存名人参加派对的时间
for i in range(n): #逐个输入名人参加和离开派对的时间(整数)
sched.append( ③
#结果如sched=[(6,7),(6,8),(6,12),(7,8),(7,1θ)]
bestTimeToParty(sched)
(1)案例中输入名人参加派对的次序发生变化,是否影响最终输出结果
如:sched=[(6,7),(6,8),(6,12),(7,8),(7,10]) 改为 sched=[(6,8),(6,12),(6,7),(7,8),(7,10)]
(2)在划线处填入合适的代码
(3)加框处代码有误,请修改

同类型试题

y = sin x, x∈R, y∈[–1,1],周期为2π,函数图像以 x = (π/2) + kπ 为对称轴
y = arcsin x, x∈[–1,1], y∈[–π/2,π/2]
sin x = 0 ←→ arcsin x = 0
sin x = 1/2 ←→ arcsin x = π/6
sin x = √2/2 ←→ arcsin x = π/4
sin x = 1 ←→ arcsin x = π/2


y = sin x, x∈R, y∈[–1,1],周期为2π,函数图像以 x = (π/2) + kπ 为对称轴
y = arcsin x, x∈[–1,1], y∈[–π/2,π/2]
sin x = 0 ←→ arcsin x = 0
sin x = 1/2 ←→ arcsin x = π/6
sin x = √2/2 ←→ arcsin x = π/4
sin x = 1 ←→ arcsin x = π/2

