寻找“多米诺骨牌组合”算法

发布于 2024-08-20 11:55:45 字数 396 浏览 2 评论 0原文

我将完成我作为编码员的学徒期,并且我有一个不错的 j2me 项目可以参与,但我必须承认我并不像我希望的那样擅长数学算法。

我的问题是从给定的一组值创建所有可能的“多米诺骨牌对”。 例如:可能的值从 0 到 6。现在想象一些具有这些值的多米诺骨牌。输出应该是这样的:

00
01
02
03
04
05
06
11
12
13
...

每对仅出现一次,但具有两个相等值的对也是可能的。

我已经搜索过这个问题,但要么我没有找到这个特定问题的解决方案,要么我并不真正理解算法是如何工作的。

我真的很感激任何解释和算法。也请随意发布替代解决方案。我不仅希望有一个解决方案,而且还希望理解它;)

I'm going to complete my apprenticeship as coder and I got a nice j2me project to work on, but I have to admit that I'm not that good with mathematical algorithms as I'd like to be.

My problem is to create all possible "domino pairs" from a given set of values.
For example: The possible values go from 0 to 6. Now imagine some domino tokens with those values. The output should then be something like this:

00
01
02
03
04
05
06
11
12
13
...

Each pair only appears a single time but pairs with two equal values are possible.

I've already searched for this problem, but either I didn't find a solution for this particular problem or I didn't really understand how the algorithms work.

I'd really appreciate any explanations and algorithms. Feel free to post alternative solutions as well. I'd prefer to not just have a solution, but to understand it as well ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2024-08-27 11:55:45

伪代码:

for i from 0 to n inclusive
   for j from i to n inclusive
       output i,j

重要的一点是第二个循环不是从零开始。这意味着我们不必测试多米诺骨牌是否已被使用。我们知道,由于该算法的构造方式,使用该算法生成的所有多米诺骨牌都是唯一的。

Pseudo code:

for i from 0 to n inclusive
   for j from i to n inclusive
       output i,j

The important point is that the second loop doesn't start from zero. This means we don't have to test if a domino has already been used. We know that all dominos produced using this algorithm are unique because of the way the algorithm is constructed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文