排列組合(概念、示例、C++程式)


排列和組合指的是數學中物件排列的順序。

排列 - 在排列中,順序很重要。因此,以特定順序排列物件稱為排列。

排列分為兩種型別:

重複排列

假設我們要生成一個三位數的程式碼。一些可能的數字是 123、897、557、333、000 和 001。那麼我們可以生成多少個這樣的數字呢?

讓我們這樣來看:

在個位數上,我們有十個選項 - 0-9

同樣,在十位和百位上,我們也有十個選項。0-9。

因此,我們擁有的總選項數為 10*10*10 = 1000。

所以,我們可以使用數字的重複生成 1000 種不同的排列。

泛化 - 如果我們有 n 個選擇和 r 個位置需要填充,我們可以生成 n*n*n…(r 次) 個排列。

因此,重複排列的公式為 nr。

不重複排列

現在,我們必須生成一個不重複數字的三位數程式碼。

例如,123、019、345、876 等。

在個位數上,我們有十個選項 - 0-9。

在十位數上,我們有九個選項 - 0-9(不包括個位數的數字)

在百位數上,我們有八個選項。

因此,我們擁有的總選項數為 8*9*10。

階乘

一個數的階乘是指小於或等於該數的所有正整數的乘積。

它用數字後面的感嘆號表示。

例如:

1! = 1.
2! = 2*1 = 2.
3! = 3*2*1 = 6.
4! = 4*3*2*1 = 24.
OR 
4! = 4*3! = 24
Hence n! = n*(n-1)!

現在,如果我們想計算 10*9*8,我們可以寫成:

$$\frac{10!}{\lgroup 10-3\rgroup!}=\frac{10!}{7!}$$

$$=\frac{10*9*8*7!}{7!}$$

$$=10*9*8$$

因此,我們可以將不重複排列的數表示為:

$$\frac{n!}{\lgroup n-r\rgroup!}$$

其中我們需要從總共 n 個事物中選擇 r 次。

符號

$$P\lgroup n,r\rgroup = nPr = nPr = \frac{n!}{\lgroup n-r\rgroup!}$$

需要記住的要點

  • nP0 = 1

  • nP1 = n

  • nPn-1 = n!

組合

在組合中,我們選擇專案,而順序無關緊要。我們可以將組合理解為從總共 n 個物件中取出 k 個物件,並且不重複。

示例

Consider that we want to place ‘123’ in some order.
The possibilities are 123, 132, 321, 312, 213, and 231.
That is, there are 3! = 3*2*1 = 6 possibilities.

現在,如果順序無關緊要,則只有一種可能性,即:選擇所有三個 '123' 並選擇它們。

公式

因此,我們調整排列公式以減少物件可以按順序排列的方式(因為我們不再關心它們的順序):

$$\frac{n!}{r!\lgroup n-r\rgroup!}$$

其中 n 是要從中選擇的專案數,r 是我們選擇的專案數。順序無關緊要,並且不允許重複。

符號

$$C\lgroup n,r\rgroup = nCr = nCr =\frac{n!}{r!(n-r)!}$$

示例問題

Q1. 在 MISSISSIPPI 中字母的不同排列中,有幾個排列四個 'I' 不在一起?

解決方案

Given word: – MISSISSIPPI
M – 1
I – 4
S – 4
P – 2
Number of permutations = 11!/(4! 4! 2!) = (11 × 10 × 9 × 8 × 7 × 6 × 5 × 4!)/ (4! × 24 × 2)
= 34650
Now, we will remove the permutations in which the four ‘I’(s) come together.
We take that 4 I’(s) come together, and they are treated as 1 letter,
∴ Total number of letters=11 – 4 + 1 = 8
⇒ Number of permutations = 8!/(4! 2!)
= (8 × 7 × 6 × 5 × 4!)/ (4! × 2)
= 840
Therefore, the total number of permutations where four 'I'(s) don’t come together = 34650 – 840 = 33810

Q2. 一個小組由 4 個女孩和 7 個男孩組成。如果團隊有:

  • 沒有女孩

  • 至少一個男孩和一個女孩

  • 至少三個女孩

解決方案

Given,
Number of girls = 7
Number of boys = 7
  • 沒有女孩

Total number of ways the team can have no girls = 4C0 × 7C5
= 1 × 21
= 21
  • 至少一個男孩和一個女孩

1 boy and 4 girls = 7C1 × 4C4 = 7 × 1 = 7
2 boys and 3 girls = 7C2 × 4C3 = 21 × 4 = 84
3 boys and 2 girls = 7C3 × 4C2 = 35 × 6 = 210
4 boys and 1 girl = 7C4 × 4C1 = 35 × 4 = 140
Total number of ways the team can have at least one boy and one girl 
= 7 + 84 + 210 + 140
= 441
  • 至少三個女孩

Total number of ways the team can have at least three girls = 4C3 × 7C2 + 4C4 × 7C1
= 4 × 21 + 7
= 84 + 7
= 91

為了找到排列和組合,我們需要找到數字的階乘。因此,用於查詢數字的階乘的函式如下:

//Function to find the factorial of a number.
int factorial(int n) {
   if (n == 0 || n == 1){
      return 1;
   }
   //Recursive call
   return n * factorial(n - 1);
}

示例

查詢不重複排列數的 C++ 程式

現在,我們可以利用上述函式和公式來計算排列和組合:

#include <bits/stdc++.h>
using namespace std;
//Function to find the factorial of a number.
int factorial(int n) {
   if (n == 0 || n == 1){
       return 1;
   }
   //Recursive call
   return n * factorial(n - 1);
}
//Driver Code
int main() {
   int n = 4, r = 2;
   //Calculating the combinations using the formula
   int combinations = factorial(n) / (factorial(r) * factorial(n-r));
   cout << "The number of Combinations is : " << combinations<<endl;
   //Calculating the permutations using the formula
   int permutations = factorial(n) / factorial(n-r);
   cout << "The number of Permutations is : " << permutations<<endl;
   return 0;
}

輸出

對於輸入 n=4、r=2,上述 C++ 程式將產生以下輸出:

The number of Combinations is : 6
The number of Permutations is : 12

結論

在這篇文章中,我們瞭解了排列和組合的概念。我們看到了公式、符號以及一些示例和示例問題。最後,我們還看到了 C++ 中的程式。

更新於:2023年8月24日

701 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.