洛谷 P2141 珠心算测验 python题解

题目链接:https://www.luogu.org/problem/P2141
思路:把数据存在list中在set中也备份一下,两个for循环计算相加数字,在set中存在也就是符合题意的数据再存到另一个set中,最后set的长度就是我们要的ans
代码:

// An highlighted block
n=input().split()
l=input().split()

s=set(l)
l=list(s)
ans=set(l)
cnt=0
ans.clear()
for i in range(0,len(l)):
    x=int(l[i])
    for j in range(i+1,len(l)):
        y=int(l[j])
        if str(x+y) in s:
            if str(x+y) in ans:
                continue
            else:
                cnt+=1
                ans.add(str(x+y))


print (len(ans))

以上終わりました。ご観覧ありがとうございます。

发布了36 篇原创文章 · 获赞 29 · 访问量 3978

猜你喜欢

转载自blog.csdn.net/YUK_103/article/details/97643699
今日推荐