#include<iostream>
#include<stdio.h>
#define MAX 50
using namespace std;
int num = 0; //正整数
int n = 0;
int a[MAX][MAX] = { 0 }; //关系矩阵
int b[MAX] = { 0 }; //存1到num中能整除num的数字
int gcd(int x, int y) //求最大公约数
{
int r;
if (x < y)
{
r = x;
x = y;
y = r;
}
r = 1;
while (r)
{
r = x % y;
x = y;
y = r;
}
return x;
}
int lcm(int x, int y) //最小公倍数
{
return x * y / gcd(x, y);
}
void calPianXu() //计算偏序集元素
{
for (int i = 1; i < num; i++) //储存偏序集元素
{
if (num%i == 0)
{
b[n++] = i;
}
}
b[n] = num;
}
void calGuanxi() //计算关系矩阵
{
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
{
if (b[i] % b[j] == 0)
a[i][j] = 1;
}
}
void calGaizhu() //计算并输出盖住集
{
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++)
{
a[k][k] = 0;
if (a[i][j] && a[j][k])
a[i][k] = 0;
}
a[n][n] = 1;
cout << "\n盖住集:{ ";
for (int i = 0; i <= n; i++) //输出盖住集
for (int j = 0; j <= n; j++)
{
if (a[i][j])
cout << "<" << b[j] << "," << b[i] << "> ";
}
cout << "}" << endl << endl;
}
void judge() //判断是否为补格
{
int flag[MAX] = { 0 }; //存储是否存在补元
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (gcd(b[i], b[j]) == 1 && lcm(b[i], b[j]) == num) //最大公约数和最小公倍数
{
flag[i] = 1;
printf("%d的补元素是%d;\n", b[i], b[j]);
break;
}
}
}
for (int i = 1; i < n; i++)
if (!flag[i])
{
cout << "\n不是有补格";
return;
}
cout << "\n是有补格";
}
int main()
{
n = 0;
cout << "请输入一个正整数:";
cin >> num;
calPianXu();
calGuanxi();
calGaizhu();
judge();
cout << endl;
return 0;
}
大二(上)离散数学 有补格的判定
最新推荐文章于 2024-05-14 17:59:16 发布