【发布时间】:2014-03-27 07:59:52
【问题描述】:
我是算法设计的新手。我有一个大约 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 个距离值存储在一个数组中。下面的代码 sn-p 使前两个相对距离数组正确,但第三个重复了一个之前已经使用过的值
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
是否有该算法的示例或实现?
【问题讨论】:
-
您好,欢迎您!好像您收到了一份学校作业,要求我们为您解决整个问题,或者只是找一个图书馆。尽管如此,我们喜欢一个很好的挑战,并尝试以最好的方式提供帮助。有些问题需要您首先自己解决这个问题。如果您可以发布代码的 sn-p 或研究证明,说明您尝试过哪些解决方案以及哪些有效/无效,并发布堆栈跟踪、输出或只是什么时间的描述,那将会很有帮助错误有时就足够了。
-
这里问的是什么有点不清楚;您是否已有算法但不了解其内部工作原理,或者您正在寻找解决固定问题的算法?
-
@Codor:我正在寻找一种算法来解决设施位置问题
-
不应该是“6. 17”,然后是“7. 23”吗?
-
提示:注意每个数字如何与下一个数字相关(对于序列 2-3-4-5-6-7-8-9-10,模式是什么?)。在这里你不要看每条线,而是看对角线。