假设一个数字大于另一个数字,则 2 个数字的可能结果数

发布于 2024-08-17 23:13:35 字数 195 浏览 2 评论 0原文

我正在尝试编写一个算法来计算结果。但我需要组合学方面的帮助。

假设我必须从 1 到 10 中选择 2 个数字。 从计数的基本规则来看,在没有任何限制的情况下,可能的结果数量为 10 * 10 = 100。(选择第一个数字的 10 个可能的结果 x 选择第二个数字的 10 个可能的结果)。

假设第一个数字必须大于第二个数字,可能的结果有多少?

I am trying to write an algorithm to calculate outcomes. But I need help with combinatorics.

Suppose I have to choose 2 numbers from 1 to 10.
From the fundamental rule of counting, in the absence of any restriction, the number of possible outcomes is 10 * 10 = 100. (10 possible outcomes in choosing the first number x 10 possible outcomes in choosing the second).

What is the number of possible outcomes given that the first number has to be greater than the second number?

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

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

发布评论

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

评论(4

路弥 2024-08-24 23:13:35

45.

想象一下你的网格由 10x10 对组成。从 1,1 到 10,10 有一条对角线,其中 A=B,所以这些不是 A>B。该线将其他情况分为两半。一半是 A>B,另一半是 B < B。 A. 每个分区的大小都相同。还剩下 90 个值(对角线取 10 个),因此有 45 个对,其中 A > A。 B.

45.

Imagine your grid of 10x10 pairs. There's a diagonal line from 1,1 to 10,10 where A=B, so those aren't A>B. The line divides the other cases into two halves.. one with A>B and one with B < A. Each of these partitions are equal size. There's 90 values left (10 taken by the diagonal line) so there are 45 pairs where A > B.

笑着哭最痛 2024-08-24 23:13:35

如果你选择:

  • 1: 2 到 10 = 9 种可能性
  • 2: 3 到 10 = 8 种可能性
  • ...
  • 9: 10 = 1 种可能性

所以

9 + 8 + ... + 2 + 1 = 45

这被称为 算术级数总和等于:

f(n) = n * (n+1) / 2 = 9 * 10 / 2 = 45

If you choose:

  • 1: 2 to 10 = 9 possibilities
  • 2: 3 to 10 = 8 possibilities
  • ...
  • 9: 10 = 1 possibility

So

9 + 8 + ... + 2 + 1 = 45

This is called an arithmetic progression the sum is equal to:

f(n) = n * (n+1) / 2 = 9 * 10 / 2 = 45
夏尔 2024-08-24 23:13:35

我想说

f(n) = n * (n-1) / 2

其中 n 等于需要组合的不同数字的数量。
因此,对于 n = 10 来说,它将是:

10 * (10-1) / 2 = 45

I would say

f(n) = n * (n-1) / 2

where n is equal to the number of different numbers that needs to be combined.
So for n = 10 it would be:

10 * (10-1) / 2 = 45
独自←快乐 2024-08-24 23:13:35

你的问题属于二项式系数。要计算 10 个项目(一次取 2 个)的独特组合总数,公式为 N! /(K!(N - K)!) 可以使用。将 N 代入 10,将 K 代入 2,得到 45。

我编写了一个类来处理处理二项式系数的常用函数,二项式系数属于此类问题。它执行以下任务:

  1. 以良好的格式将任意 N 选择 K 的所有 K 索引输出到文件中。 K 索引可以替换为更具描述性的字符串或字母。这种方法使得解决此类问题变得非常简单。

  2. 将 K 索引转换为排序二项式系数表中条目的正确索引。该技术比依赖迭代的旧发布技术要快得多。它通过使用帕斯卡三角形固有的数学属性来实现这一点。我的论文谈到了这一点。我相信我是第一个发现并发布此技术的人,但我可能是错的。

  3. 将排序二项式系数表中的索引转换为相应的 K 索引。

  4. 使用Mark Dominus方法来计算二项式系数,这种方法不太可能溢出并适用于更大的数字。

  5. 该类是用 .NET C# 编写的,并提供了一种使用通用列表来管理与问题相关的对象(如果有)的方法。该类的构造函数采用一个名为 InitTable 的布尔值,当该值为 true 时,将创建一个通用列表来保存要管理的对象。如果该值为 false,则不会创建该表。执行上述 4 种方法不需要创建该表。提供了访问器方法来访问表。

  6. 有一个关联的测试类,它显示了如何使用该类及其方法。它已经过 2 个案例的广泛测试,没有已知的错误。

要了解此类并下载代码,请参阅制表二项式系数

将此类转换为 C++ 应该不难。

Your question falls under the binomial coefficient. To calculate the total number of unique combinations of 10 items taken 2 at a time, the formula N! / (K! (N - K)!) can be used. Plugging in 10 for N and 2 for K yields 45.

I have written a class to handle common functions for working with the binomial coefficient, which is the type of problem this falls under. It performs the following tasks:

  1. Outputs all the K-indexes in a nice format for any N choose K to a file. The K-indexes can be substituted with more descriptive strings or letters. This method makes solving this type of problem quite trivial.

  2. Converts the K-indexes to the proper index of an entry in the sorted binomial coefficient table. This technique is much faster than older published techniques that rely on iteration. It does this by using a mathematical property inherent in Pascal's Triangle. My paper talks about this. I believe I am the first to discover and publish this technique, but I could be wrong.

  3. Converts the index in a sorted binomial coefficient table to the corresponding K-indexes.

  4. Uses Mark Dominus method to calculate the binomial coefficient, which is much less likely to overflow and works with larger numbers.

  5. The class is written in .NET C# and provides a way to manage the objects related to the problem (if any) by using a generic list. The constructor of this class takes a bool value called InitTable that when true will create a generic list to hold the objects to be managed. If this value is false, then it will not create the table. The table does not need to be created in order to perform the 4 above methods. Accessor methods are provided to access the table.

  6. There is an associated test class which shows how to use the class and its methods. It has been extensively tested with 2 cases and there are no known bugs.

To read about this class and download the code, see Tablizing The Binomial Coeffieicent.

It should not be hard to convert this class to C++.

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