首頁  >  文章  >  後端開發  >  握手次數,每個人只握一次手

握手次數,每個人只握一次手

王林
王林轉載
2023-08-29 18:57:03610瀏覽

握手次數,每個人只握一次手

假設你在一個社交聚會。如果你只握手一次,你能計算出你能做多少次握手嗎?這個問題可能會讓你感到有趣。這個問題可以透過使用排列組合的數學方法來解決。然而,數學運算可能會耗費時間。

在本文中,我們將討論如何使用C 解決這個問題。我們將探討不同的方法,包括數學公式、遞歸和其他組合技術。

輸入輸出場景

Suppose you have N number of people in a gathering. You want to calculate the number of handshakes possible such that a person shakes hands only once.

Input: N = 16
Output: 120
Input: N = 11
Output: 55

Using the Formula for Handshakes

The formula for finding the number of handshakes in a gathering of N people is −

No. of handshakes = N *(N-1) /2

Each of the N people will shake the hands with (N-1) individuals (excluding the person itself) and the handshakes between two individuals is not counted twice.

For Example, if the number of individuals is 14. Then, number of handshakes are

#
Handshakes = 14 * (14 - 1)/ 2
           = 14 * 13 / 2
           = 182/2
           = 91

Example

在下面的範例中,我們使用的是計算握手次數的公式。在這裡,我們只需使用數學運算符,以聚會上的人數作為輸入。

#include <iostream>
using namespace std;

int count(int N) {
   // Formula: N * (N-1) / 2
   return (N * (N - 1)) / 2;
}

int main() {
   int totalIndividuals= 10;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

輸出

Number of handshakes: 45

使用for迴圈

在這裡,我們透過從1迭代到‘N-1’並將所有值相加來計算握手的次數。

Example

#include <iostream>
using namespace std;

int count(int N) {
   int numHandshakes = 0;

   for (int x = 1; x < N; x++) {
      numHandshakes += x;
   }

   return numHandshakes;
}

int main() {
   int totalIndividuals = 10;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

輸出

Number of handshakes: 45

使用遞迴

We can use recursion for calculating the number of handshakes. By doing so, we divide the problem into smaller problems by considering one person at a time.

Example

#include <iostream>
using namespace std;

int count(int N) {
   if (N <= 1)
      return 0;
   return (N - 1) + count(N - 1);
}

int main() {
   int totalIndividuals = 20;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

輸出

Number of handshakes: 190

使用While迴圈

在這裡,我們使用了一個while循環,它具有一個遞減的計數器來計算握手的次數。循環從總人數開始,然後在每次迭代後逐一減少計數器。

Example

#include <iostream>
using namespace std;

int count(int N) {
   int numHandshakes = 0;

   while (N > 1) {
      numHandshakes += N - 1;
      N--;
   }

   return numHandshakes;
}

int main() {
   int totalIndividuals = 16;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

輸出

Number of handshakes: 120

使用動態規劃

Here, we have used dynamic programming for the calculation.

  • Initialize a ‘dp’ vector to store the number of handshakes.

  • #Iterate from 1 to N. In each iteration, it declares the number of handshakes as the sum of previous handshakes and the present number of individual minus 1.

Example

#include <iostream>
#include <vector>
using namespace std;

int count(int N) {
   std::vector<int> dp(N + 1);
   dp[0] = 0;

   for (int x = 1; x <= N; x++) {
      dp[x] = dp[x - 1] + (x - 1);
   }

   return dp[N];
}

int main() {
   int totalIndividuals = 21;
   int numHandshakes = count(totalIndividuals);
   std::cout << "Number of handshakes: " << numHandshakes << std::endl;
   return 0;
}

輸出

Number of handshakes: 210

注意  此方法有助於避免冗餘計算。在這裡,我們將先前計算的值儲存在「dp」向量中,您可以隨時存取並重複使用它。這使得演算法高效,並且減少了總體計算時間。

Conclusion

我們已經討論了各種方法,可以計算出一個人只握手一次的握手次數。這些方法包括使用數學運算子進行公式計算,使用for循環,遞歸,while循環和動態規劃。每種方法都有其優點。動態規劃是一種更有系統和有組織的解決問題的方法。根據具體要求,您可以使用任何一種方法。

以上是握手次數,每個人只握一次手的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除