大于数字的唯一和



因此,我正试图编写一个代码,该代码采用1到10之间的数字列表,并找到大于或等于10的和,然后从列表中删除这些数字。这里有一个转折点:首先你需要检查列表中是否有任何10,任何一对加起来为10的数字,然后是任何3个数字,以此类推,直到5。此外,数字总和越低越好。所以在求和夫妻时,你需要去掉大多数数字。到目前为止,我成功地完成了配对:

n = input("How many numbers in the list? n")
throw = []
for i in range(int(n)):
throw.append(random.randint(1, 10))
throw.sort()
increments = 0
print(throw)
increments += throw.count(10)
throw = list(filter(lambda i: i != 10, throw))
high = len(throw)-1
low = 0
acceptable_couples = []
get_rid = []
while low < high:
sums = throw[high] + throw[low]
if sums >= 10:
increments += 1
get_rid.append(throw[high])
get_rid.append(throw[low])
acceptable_couples.append((throw[high], throw[low]))
high -= 1
low += 1
else:
low += 1
for i in get_rid:
throw.remove(i)

我也做了3对,并考虑对4和5:应用相同的方法

while len(throw) >= 3:
z = 0
x = list(itertools.combinations(throw, 3))
for couple in x:
if sum(couple) >= 10:
z += 1
i = list(couple)
increments += 1
for j in couple:
throw.remove(j)
break
else:
continue
if z == 0:
break

我希望能找到一种不那么混乱的方法。这很有效,但对于大量的数字来说,这似乎是很多无用的计算。有什么想法吗?

在这种情况下,这可能是一个解决方案。在伪代码中:

  1. 读取数字列表并从高到低排序
  2. 从列表中删除所有数字10,并将其作为解决方案添加
  3. 循环直到列表为空
  4. 取列表中最大的数字(这是列表排序时的第一个数字(并将其从列表中删除
  5. 检查通过加最小的数字,我们是否可以得到大于或等于10的和,如果在完成循环之前不加更大的数字。如果找到解决方案,则中断循环,将其添加到解决方案中,然后从解决该解决方案的列表中删除该元素
  6. 如果已解决,则中断并重复外部while循环,并使用列表中的下一个最大数字。如果没有解决,那么将下一个最大的数字添加到总和中,并将其从列表中删除,然后检查是否可以找到添加较小数字的解决方案,再次运行for循环

编码

import random
n = int(input("How many numbers in the list? n"))
throw = [random.randint(1, 6) for _ in range(n)]
print(throw)
throw.sort(reverse=True)
print(throw)
list_of_solved_sets = []
# remove all numbers 10 from the list and add a solution
while throw and throw[0] == 10:
list_of_solved_sets.append([throw[0]])
del throw[0]
while throw:
# take the largest number in the set (which is the first number 
# as list is sorted) and remove it from the list
solved_set = [throw[0]]
_sum = throw[0]
del throw[0]
while throw:
solved = False
# Check if by adding the smallest number we can get
# a sum greater or equal to 10 if not go to a bigger number
# until we complete the loop. Break loop if a solution is found, add
# to solutions and remove element from the list that solved it
for j in range(len(throw) - 1, -1, -1):
if _sum + throw[j] >= 10:
solved_set.append(throw[j])
del throw[j]
list_of_solved_sets.append(solved_set)
solved = True
break
# if solved break and repeat the outer while loop with next biggest number
# in the list. If not solved then add the next biggest number to the sum and      
# check if we can find a solution adding smaller numbers running the for 
#loop again
if solved:
break
else:
_sum += throw[0]
solved_set.append(throw[0])
del throw[0]
print('List of solved sets:')
for i, solved_set in enumerate(list_of_solved_sets):
print(f'   {i+1}: {solved_set}')

结果示例:

How many numbers in the list?
10
[3, 2, 5, 1, 6, 3, 4, 3, 2, 1]
[6, 5, 4, 3, 3, 3, 2, 2, 1, 1]
List of solved sets:
1: [6, 4]
2: [5, 3, 2]
3: [3, 3, 2, 1, 1]

不确定我是否正确理解你的问题。我的解释是,你有一组1到10之间的数字,你需要从这个数字集中找到最长的数字子集,这些数字的总和达到10或更大,然后从原始数字集中删除这些数字,直到你找不到更多的数字集。

如果这是正确的解释,那么以下内容应该有效:

import random
n = int(input("How many numbers in the list? n"))
throw = [random.randint(1, 10) for _ in range(n)]
print(throw)
throw.sort()
list_of_solved_sets = []
sets_found = True
while sets_found:
_sum = 0
solved_set = []
solved_indexes = []
for index, element in enumerate(throw):
_sum += element
solved_set.append(element)
solved_indexes.append(index)
if _sum >= 10:
break
if len(solved_set) <= 1:
sets_found = False
else:
list_of_solved_sets.append(solved_set)
for index in reversed(solved_indexes):
del throw[index]
print('List of solved sets:')
for i, solved_set in enumerate(list_of_solved_sets):
print(f'   {i+1}: {solved_set}')

请注意,您需要以相反的顺序从列表throw中删除索引!

最新更新