Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想生成一个 3*6 的数字,它可以形成一个 3rows*6cols 的数组。但我不希望在任何列中找到重复项。它们可以在任何地方,在同一行中,但在每一列中,不应有相同数字的任何重复项。有没有生成它的算法?
按列填充矩阵并使用 Set 来跟踪您已在当前列中使用的数字。您可以在伪代码中为每个单元格使用 do-while 语句:
for each column { used = new set for each cell in column { do { num = generate random number } while (num already in used) add num to used add num to the current cell } }