握手次数,每个人只握一次手

握手次数,每个人只握一次手

假设你在一个社交聚会中。如果你只握手一次,你能计算出你能做多少次握手吗?这个问题可能让你感到有趣。这个问题可以通过使用排列组合的数学方法来解决。然而,数学运算可能会耗费时间。

在本文中,我们将讨论如何使用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.

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

登录后复制

Using the Formula for Handshakes

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

  1. 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

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

登录后复制

Example

在下面的示例中,我们使用的是计算握手次数的公式。在这里,我们只需使用数学运算符,以聚会上的人数作为输入。

  1. #include 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: 45
  2. 登录后复制

  3. 使用for循环

  4. 在这里,我们通过从1迭代到‘N-1’并将所有值相加来计算握手的次数。

  5. Example

  6. #include using namespace std;int count(int N) {   int numHandshakes = 0;   for (int x = 1; x 

    输出

    Number of handshakes: 45
  7. 登录后复制

  8. 使用递归

  9. 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.

  10. Example

  11. #include using namespace std;int count(int N) {   if (N 

    输出

    Number of handshakes: 190
  12. 登录后复制

  13. 使用While循环

  14. 在这里,我们使用了一个while循环,它具有一个递减的计数器来计算握手的次数。循环从总人数开始,然后在每次迭代后逐个减少计数器。

  15. Example

  16. #include 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: 120
  17. 登录后复制

  18. 使用动态规划

  19. Here, we have used dynamic programming for the calculation.

  20. Initialize a dp vector to store the number of handshakes.

  21. 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.

  22. Example

  23. #include #include using namespace std;int count(int N) {   std::vector dp(N + 1);   dp[0] = 0;   for (int x = 1; x 

    输出

    Number of handshakes: 210
  24. 登录后复制

  25. 注意  这种方法有助于避免冗余计算。在这里,我们将先前计算的值存储在“dp”向量中,您可以随时访问并重复使用它。这使得算法高效,并且减少了总体计算时间。

  26. Conclusion

  27. 我们已经讨论了各种方法,可以计算出一个人只握手一次的握手次数。这些方法包括使用数学运算符进行公式计算,使用for循环,递归,while循环和动态规划。每种方法都有其优点。动态规划是一种更系统和有组织的解决问题的方法。根据具体要求,您可以使用任何一种方法。

  28. 以上就是握手次数,每个人只握一次手的详细内容,更多请关注【创想鸟】其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

点点赞赏,手留余香

给TA打赏
共0人
还没有人赞赏,快来当第一个赞赏的人吧!
    编程技术

    使用交换最小化两个数组中最大数的乘积

    2025-3-6 14:58:54

    编程技术

    一个包含n个元素且具有O(1)操作的数据结构?

    2025-3-6 14:59:02

    0 条回复 A文章作者 M管理员
    欢迎您,新朋友,感谢参与互动!
      暂无讨论,说说你的看法吧
    个人中心
    购物车
    优惠劵
    今日签到
    私信列表
    搜索