网易第一题ac 以及第三题90% 代码
第一题 给定一个数组,是否存在一个x,使得数组中某些元素加上x,某些元素减去x之后,数组中的所有元素都相等 ac
# -*- coding=utf-8 -*-
def check_equal(seq):
length = len(seq)
if length <= 2:
return 'YES'
if length != 3:
return 'NO'
seq.sort()
a, b, c = seq
if b - a == c - b:
return 'YES'
else:
return 'NO'
if __name__ == "__main__":
seq_nums = int(input())
for i in range(seq_nums):
length = int(input())
seq = list(map(int, input().split()))
seq = list(set(seq))
print(check_equal(seq))
第三题 求给定所有数对中,能够整除每个数对中最少一个元素的最大质数x 通过了90%
# -*- coding=utf-8 -*-
from math import sqrt
def get_max_gcd(seq):
# 得到质因数上线
min_value = min(seq, key=lambda x: max(x[0], x[1]))
min_value = min(min_value)
zhi_list = get_zhi(min_value)
for zhi in zhi_list:
flag = True
for item in seq:
if not check_equal(item, zhi):
flag = False
break
if flag:
return zhi
return -1
def check_equal(item, zhi):
for _ in item:
if _ % zhi == 0:
return True
return False
def get_zhi(value):
sqrt_value = int(sqrt(value)) + 1
res = [2]
i = 3
while i <= value:
flag = True
j = 0
while j < len(res) and res[j] <= sqrt_value:
if i % res[j] == 0:
flag = False
break
else:
j += 1
if flag:
res.append(i)
i += 1
return res[::-1]
if __name__ == "__main__":
n = int(input())
seq = []
for i in range(n):
a, b = map(int, input().split())
seq.append((a, b))
print(get_max_gcd(seq))
#网易#