目录
输入输出场景
Using the Formula for Handshakes
Example
输出
使用for循环
使用递归
使用While循环
使用动态规划
Conclusion
首页 后端开发 C++ 握手次数,每个人只握一次手

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

Aug 29, 2023 pm 06:57 PM
握手次数 人次关系

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

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

在本文中,我们将讨论如何使用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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

C语言数据结构:树和图的数据表示与操作 C语言数据结构:树和图的数据表示与操作 Apr 04, 2025 am 11:18 AM

C语言数据结构:树和图的数据表示与操作树是一个层次结构的数据结构由节点组成,每个节点包含一个数据元素和指向其子节点的指针二叉树是一种特殊类型的树,其中每个节点最多有两个子节点数据表示structTreeNode{intdata;structTreeNode*left;structTreeNode*right;};操作创建树遍历树(先序、中序、后序)搜索树插入节点删除节点图是一个集合的数据结构,其中的元素是顶点,它们通过边连接在一起边可以是带权或无权的数据表示邻

C语言文件操作难题的幕后真相 C语言文件操作难题的幕后真相 Apr 04, 2025 am 11:24 AM

文件操作难题的真相:文件打开失败:权限不足、路径错误、文件被占用。数据写入失败:缓冲区已满、文件不可写、磁盘空间不足。其他常见问题:文件遍历缓慢、文本文件编码不正确、二进制文件读取错误。

c上标3下标5怎么算 c上标3下标5算法教程 c上标3下标5怎么算 c上标3下标5算法教程 Apr 03, 2025 pm 10:33 PM

C35 的计算本质上是组合数学,代表从 5 个元素中选择 3 个的组合数,其计算公式为 C53 = 5! / (3! * 2!),可通过循环避免直接计算阶乘以提高效率和避免溢出。另外,理解组合的本质和掌握高效的计算方法对于解决概率统计、密码学、算法设计等领域的许多问题至关重要。

c语言函数的基本要求有哪些 c语言函数的基本要求有哪些 Apr 03, 2025 pm 10:06 PM

C语言函数是代码模块化和程序搭建的基础。它们由声明(函数头)和定义(函数体)组成。C语言默认使用值传递参数,但也可使用地址传递修改外部变量。函数可以有返回值或无返回值,返回值类型必须与声明一致。函数命名应清晰易懂,使用驼峰或下划线命名法。遵循单一职责原则,保持函数简洁性,以提高可维护性和可读性。

c语言函数名定义 c语言函数名定义 Apr 03, 2025 pm 10:03 PM

C语言函数名定义包括:返回值类型、函数名、参数列表和函数体。函数名应清晰、简洁、统一风格,避免与关键字冲突。函数名具有作用域,可在声明后使用。函数指针允许将函数作为参数传递或赋值。常见错误包括命名冲突、参数类型不匹配和未声明的函数。性能优化重点在函数设计和实现上,而清晰、易读的代码至关重要。

C语言多线程编程:新手指南与疑难解答 C语言多线程编程:新手指南与疑难解答 Apr 04, 2025 am 10:15 AM

C语言多线程编程指南:创建线程:使用pthread_create()函数,指定线程ID、属性和线程函数。线程同步:通过互斥锁、信号量和条件变量防止数据竞争。实战案例:使用多线程计算斐波那契数,将任务分配给多个线程并同步结果。疑难解答:解决程序崩溃、线程停止响应和性能瓶颈等问题。

c语言函数的概念 c语言函数的概念 Apr 03, 2025 pm 10:09 PM

C语言函数是可重复利用的代码块,它接收输入,执行操作,返回结果,可将代码模块化提高可复用性,降低复杂度。函数内部机制包含参数传递、函数执行、返回值,整个过程涉及优化如函数内联。编写好的函数遵循单一职责原则、参数数量少、命名规范、错误处理。指针与函数结合能实现更强大的功能,如修改外部变量值。函数指针将函数作为参数传递或存储地址,用于实现动态调用函数。理解函数特性和技巧是编写高效、可维护、易理解的C语言程序的关键。

c语言如何输出倒数 c语言如何输出倒数 Apr 04, 2025 am 08:54 AM

如何在 C 语言中输出倒数?回答:使用循环语句。步骤:1. 定义变量 n 存储要输出的倒数数字;2. 使用 while 循环持续打印 n 直到 n 小于 1;3. 在循环体内,打印出 n 的值;4. 在循环末尾,将 n 减去 1 以输出下一个更小的倒数。

See all articles