可以考虑 直接枚举,当然我们可以枚举四个数字中最少的两个数,然后对另两个数做two pointer,时间复杂度为 .
不过试了一下两种方法都是无解:
a = [675.96,459.88,827.28,565.24,584.55,803.15,681.35,800.28,827.28,946.84,966.44,80.00,420.58,827.28,716.28] n = len(a) a.sort() print(a) for i in range(n - 1): for j in range(i + 1,n): k = j + 1 t = n - 1 while (k < t): s = a[k] + a[t] + a[i] + a[j] if s == 2697.41: print(a[k],a[t]) elif s > 2697.41: t -= 1 else: k += 1 for i in range(n): for j in range(i + 1,n): for k in range(j + 1,n): for t in range(k + 1,n): if a[i] + a[j] + a[k] + a[t] == 2697.41: print(a[i],a[j],a[k],a[t])