database 如何获取数据库中一组唯一对的总数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18859430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:05:12  来源:igfitidea点击:

How do I get the total number of unique pairs of a set in the database?

databasemath

提问by Kirk Ouimet

4 items:

4 项:

A
B
C
D

6 unique pairs possible:

可能有 6 对独特的配对:

AB
AC
AD
BC
BD
CD

What if I have 100 starting items? How many unique pairs are there? Is there a formula I can throw this into?

如果我有 100 个起始物品怎么办?有多少个独特的对?有没有我可以把它放进去的公式?

回答by Mike Christensen

What you're looking for is n choose k. Basically:

您正在寻找的是n 选择 k。基本上:

enter image description here

在此处输入图片说明

For every pair of 100 items, you'd have 4,950 combinations- provided order doesn't matter (AB and BA are considered a single combination) and you don't want to repeat (AA is not a valid pair).

对于每对 100 件商品,您将有 4,950 种组合- 前提是顺序无关紧要(AB 和 BA 被视为单一组合)并且您不想重复(AA 不是有效对)。

回答by audiomason

TLDR; The formula is n(n-1)/2where nis the number of items in the set.

TLDR;公式是n(n-1)/2哪里n是集合中的项目数。

Explanation:

解释:

To find the number of unique pairs in a set, where the pairs are subject to the commutative property(AB = BA), you can calculate the summationof 1 + 2 + ... + (n-1)where nis the number of items in the set.

要查找一组,其中对受独特的对数交换律(AB = BA),就可以计算出总和1 + 2 + ... + (n-1)地方n是在设定项目的数量。

The reasoning is as follows, say you have 4 items:

推理如下,假设您有 4 个项目:

A
B
C
D

The number of items that can be paired with Ais 3, or n-1:

可配对的项目数A为 3,或n-1

AB
AC
AD

It follows that the number of items that can be paired with Bis n-2(because Bhas already been paired with A):

因此可以配对的项目数Bn-2(因为B已经配对A):

BC
BD

and so on...

等等...

(n-1) + (n-2) + ... + (n-(n-1))

which is the same as

这与

1 + 2 + ... + (n-1)

or

或者

n(n-1)/2

回答by Joni

This is how you can approach these problems in general on your own:

这是您通常可以自己解决这些问题的方法:

The first of the pair can be picked in N (=100) ways. You don't want to pick this item again, so the second of the pair can be picked in N-1 (=99) ways. In total you can pick 2 items out of N in N(N-1) (= 100*99=9900) different ways.

可以通过 N (=100) 种方式选择一对中的第一个。您不想再次选择此项目,因此可以通过 N-1 (=99) 种方式选择该对中的第二个。总的来说,您可以用 N(N-1) (= 100*99=9900) 种不同的方式从 N 中挑选 2 个项目。

But hold on, this way you count also different orderings: AB and BA are both counted. Since every pair is counted twice you have to divide N(N-1) by two (the number of ways that you can order a list of two items). The number of subsets of two that you can make with a set of N is then N(N-1)/2 (= 9900/2 = 4950).

但请稍等,这样您也计算不同的排序:AB 和 BA 都计算在内。由于每对计算两次,因此您必须将 N(N-1) 除以 2(您可以对两个项目的列表进行排序的方式数)。你可以用一组 N 组成的两个子集的数量是 N(N-1)/2 (= 9900/2 = 4950)。

回答by atomsfat

I was solving this algorithmand get stuck with the pairs part.

我正在解决这个算法并陷入对部分的困境。

This explanation help me a lot https://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/

这个解释对我很有帮助 https://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/

So to calculate the sum of series of numbers:

所以要计算一系列数字的总和:

n(n+1)/2

But you need to calculate this

但是你需要计算这个

1 + 2 + ... + (n-1)

So in order to get this you can use

所以为了得到这个,你可以使用

n(n+1)/2 - n

that is equal to

这等于

n(n-1)/2