使用C++编写,找到一个集合上的自反关系的数量

在本文中,我们将解释在一个集合上找到反身关系的方法。在这个问题中,我们给出一个数字n,以及一个由n个自然数组成的集合,我们必须确定反身关系的数量。

反身关系 - 如果对于集合A中的每个'a',(a, a)属于关系R,则称关系R是集合A上的反身关系。例如 -

Input : x = 1 Output : 1 Explanation : set = { 1 }, reflexive relations on A * A : { { 1 } } Input : x = 2 Output : 4 Explanation : set = { 1,2 }, reflexive relations on A * A : { ( 1, 1 ) , ( 2, 2 ) } { ( 1, 1 ), ( 2, 2 ), ( 1, 2 ) } { ( 1, 1 ), ( 2, 2 ), ( 1, 2 ), ( 2, 1 ) } { ( 1, 1 ), ( 2, 2 ), ( 2, 1 ) }登录后复制

解决方案的方法

可以通过公式2n2−n来计算元素集上的自反关系的数量。这个通用公式是通过计算整数的自反关系数量得到的。

使用C++编写,找到一个集合上的自反关系的数量

例子

#include using namespace std; int countReflexive(int n){ int ans = 1 > n ; // taking input n from the user using std cin. int result = countReflexive(n); // calling function to calculate number of reflexive relations cout