【离散数学】实验三 偏序关系中盖住关系的求取及格论中有补格的判定

  1.实验目的

    编程实现整除关系这一偏序关系上所有盖住关系的求取,并判定对应的偏序集是否为格。

  2.实验要求

   对任意给定的正整数,利用整除关系求所有由其因子构成的集合所构成的格,判断其是否为有补格。

  3.编码思路

     将该正整数的因子保存在数组中,利用盖住关系的性质,两个数a、b之间不存在第三者c,使得a整除c,c整除b,即可求取所有盖住关系。

     整除关系对应的偏序集都是格,所以不用判断,接下来是判断有补格。利用性质,首先,格的全上界一定是输入,的正整数,全下界一定是1,所以就根据这个来判断补元。两重循环,如果每个因子都能找到另一个因子,使它们的最小公倍数为输入的正整数,最大公约数为1,那么就是有补格。

代码如下,只用了一个gcd函数:

/*
 *实验目的:编程实现整除关系这一偏序关系上所有盖住关系的求取,并判定对应的偏序集是否为格。
 *实验要求:对任意给定的正整数,利用整除关系求所有由其因子构成的集合所构成的格,判断其是否为有补格。
 *运行环境:Code::Blocks 13.12
 */

 #include <cstdlib>
 #include <cstdio>
 #include <cmath>
 #include <vector>
 #include <cstring>
 #include <map>
 #include <iostream>
 #include <algorithm>
 using namespace std;

 const int LEN = 140 + 10;
 //Variable;
 int n;
 int cnt;
 int arr[LEN];

//求最大公约数
int gcd(int n, int m);

 int main()
 {
     cout << "请输入一个整数(>0) : ";
     while(cin >> n)  //连续输入,Ctrl+Z终止
     {
         cout << endl;
         int k = (int)floor(sqrt(n) + 0.5);
         cnt  = 1;
         for(int i = 1; i <= k; i++)
         {
             if(n % i == 0)
             {
                 if(i != n/i)
                 {
                     arr[cnt++] = i;
                     arr[cnt++] = n/i;
                 }
                 else
                 {
                     arr[cnt++] = i;
                 }
             }
         }

         sort(arr, arr+cnt);  //排序

         //输出偏序集上的盖住关系
         cout << "输出偏序集上的盖住关系 : " ;
         for(int i = 1; i < cnt; i++)
         {
             for(int j = i + 1; j < cnt; j++)
             {
                 if(arr[j] % arr[i] == 0 && i < j)
                 {
                     bool flag = true;
                     for(int k = i + 1; k < j; k++)
                     {
                         if(arr[j] % arr[k] == 0 && arr[k]%arr[i] == 0 && i < k && k < j)
                         {
                             flag = false;
                             break;
                         }
                     }
                     if(flag)
                     {
                         cout << "(" << arr[i] << "," << arr[j] << ")" << "   ";
                     }
                 }
             }
         }

         cout << endl << endl;

         int brr[LEN];
         memset(brr, 0, sizeof(brr));

         for(int i = 1; i < cnt; i++)
         {
             for(int j = i + 1; j < cnt; j++)
             {
                 int temp = gcd(arr[i], arr[j]);
                 if(temp == 1 && arr[i] * arr[j] ==  n)
                 {
                     brr[i] = 1;
                     brr[j] = 1;
                     break;
                 }
                 else
                 {
                     continue;
                 }
             }
         }

         bool res = true;
         for(int i = 1; i < cnt; i++)
         {
             if(brr[i] == 0)
             {
                 res = false;
             }
         }
         if(res)
         {
             cout << "是有补格!" << endl << endl;
         }
         else
         {
             cout << "不是有补格!" << endl << endl;
         }
         memset(arr, 0, sizeof(arr));
         memset(brr, 0, sizeof(brr));

         cout << "请输入一个整数(>0) : ";
     }
     return 0;
 }

int gcd(int n, int m)
{
    if(n < m)
    {
        int temp = n;
        n = m;
        m = temp;
    }
    int remainer = n % m;
    while(remainer)
    {
        n = m;
        m = remainer;
        remainer = n % m;
    }
    return m;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/Tobyuyu/p/4965339.html