<分区>
我是算法设计的新手。我有一个大约 8128 个整数的列表。我需要创建一个算法来创建 128 种不同的唯一数字组合。
第一个数字 1 没有被任何组合使用。前 6 个数字序列开始如下:
- 2,4,7,11,16,22
- 3,6,10,15,21
- 5,9,14,20
- 8,13,19
- 12,18
- 17
我注意到数字之间的间隔在每个序列之间增加 1。我还看到它似乎选择了第一个唯一的(未使用的)整数来开始一个序列。我一直在尝试用 Python 实现它。
我想解决设施位置问题。我有 8128 个距离值存储在一个数组中。下面的代码片段使前两个相对距离数组正确,但第三个重复了之前已经使用过的值
distances = [[0 for col in range(2)] for row in range(128)] #this array contains line numbers that contain distances
#1st iteration works
startpoint = 0
count = 1
diff = 2
while startpoint < 8127:
print distance[startpoint+diff]
startpoint = startpoint+count
count += 1
diff += 2
#2nd iteration works
startpoint = 1
count = 2
diff = 3
while startpoint < 8127:
print distance[startpoint+diff]
startpoint = startpoint+count
count += 1
diff += 2
#3rd iteration repeats a value
startpoint = 2
count = 3
diff = 4
while startpoint < 8127:
print distance[startpoint+diff]
startpoint = startpoint+count
count += 1
diff += 2
是否有该算法的示例或实现?