CCF CSP 2014-3 前两题

1、相反数

# -*- coding:utf-8 -*-
# 输入整数n
n = int(input())
# 输入n个非零整数
nums = list(map(int, input().split()))
ans = 0
for i in range(n):
    for j in range(i, n):
        if nums[i] + nums[j] == 0:
            ans += 1

print(ans)

2、窗口

# -*- coding:utf-8 -*-
# N=层数,M=点击次数
N, M = map(int, input().split())

# 输入每个窗口坐标
window = []
for i in range(N):
    x1, y1, x2, y2 = list(map(int, input().split()))
    window.append([x1, y1, x2, y2, i + 1])
# print(window)

# 输入点击位置坐标
click = []
for j in range(M):
    x, y = list(map(int, input().split()))
    click.append([x, y])


# 查找点击是否在窗口内
def contain(mx: int, my: int):
    for s in range(N):
        if window[s][0] <= mx <= window[s][2] and window[s][1] <= my <= window[s][3]:
            res.append(s)


for m in range(M):
    res = []
    contain(click[m][0], click[m][1])
    if not res:
        print("IGNORED")
    else:
        ans = max(res)
        print(window[ans][-1])
        top_window = window.pop(ans)
        window.append(top_window)
# 修改思路
# 在查找点击是否在窗口内的函数contain中,你没有清空res列表,导致每次点击后res会累积之前的结果,这会影响最大值的判断。你应该在每次调用contain函数之前,把res清空为一个空列表。
# 在输出最顶层窗口编号后,你没有把这个窗口移到所有窗口的最顶层,而是只打印了它的编号。这会导致下一次点击时层次顺序不变。你应该在输出编号后,把这个窗口从window列表中删除,并重新插入到列表的末尾,表示它被移到了最顶层。
# 你可以使用window[s][-1]来表示窗口的编号,而不是window[s][4],这样更简洁。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值