判断集合关系(自反,反自反,对称,反对称,传递)


具体代码如下

  1 #include<iostream>
  2 using namespace std;
  3 
  4 void Input(int a[25][2],int s)
  5 {
  6     int i;
  7     
  8     for (i = 0; i < s; i++)
  9     {
 10         cin >> a[i][0] >> a[i][1];
 11     }
 12 }
 13 
 14 void Output(int a[5][2],int b[5][5], int n,int s)
 15 {
 16     int i;
 17     int j, k;
 18     int buff=0;
 19     int buff1, buff2;
 20     cout << endl<< "the relation is :" << endl;
 21     for (i = 0; i < s; i++)
 22     {
 23         buff1 = a[i][0] - 1;
 24         buff2 = a[i][1] - 1;
 25         b[buff1][buff2] = 1;
 26     }
 27     for (j = 0; j < n; j++)
 28     {
 29         for (k = 0; k < n; k++)
 30         {
 31             cout << b[j][k] << " ";
 32             buff++;
 33             if (buff%n == 0)
 34                 cout << endl;
 35         }
 36     }
 37     cout << endl;
 38 }
 39 void IsReflexive(int b[5][5],int n)
 40 {
 41     int i;
 42     cout << "1、Is Reflexive?" << endl<<endl;
 43     for (i = 0; i < n; i++)
 44     {
 45         if (!b[i][i])
 46         {
 47             cout << "the answer is NO!" << endl;
 48             cout << "At least can't find" << "<" << i + 1 << "," << i + 1 << ">" << endl << endl<<endl;
 49             return;
 50         }
 51     }
 52     cout << "the answer is YES!" << endl<<endl<<endl;
 53 }
 54 void IsAntireflexive(int b[5][5], int n)
 55 {
 56     int i;
 57     cout << "2、Is Antireflexive?" << endl<<endl;
 58     for (i = 0; i < n; i++)
 59     {
 60         if (b[i][i])
 61         {
 62             
 63             cout << "the answer is NO!" << endl;
 64             cout << "At least find" << "<" << i + 1 << "," << i + 1 << ">" << endl<<endl<<endl;
 65             return;
 66         }
 67     }
 68     cout << "the answer is YES!" << endl<<endl<<endl;
 69 }
 70 void IsSymmetric(int b[5][5], int n)
 71 {
 72     int i, j;
 73     cout << "3、Is Symmetric?" << endl << endl;
 74     for (i = 0; i < n; i++)
 75     {
 76         for (j = 0; j < n; j++)
 77         {
 78             if (b[i][j] != b[j][i])
 79             {
 80                 cout << "the answer is NO!" << endl;
 81                 cout << "At least both of" << "<" << i + 1 << "," << j + 1 << ">" << "and" << "<" << j + 1 << "," << i + 1 << ">" << "not appear together" << endl << endl << endl;
 82                 return;
 83             }
 84         }
 85         
 86     }
 87     cout << "the answer is YES!" << endl << endl << endl;
 88 }
 89 void IsAntisymmetry(int b[5][5], int n)
 90 {
 91     int i, j;
 92     cout << "4、Is Antisymmetry?" << endl << endl;
 93     for (i = 0; i < n; i++)
 94     {
 95         for (j = 0; j < n; j++)
 96         {
 97             if (b[i][j] && b[i][j] == b[j][i] && i != j)
 98             {
 99                 cout << "the answer is NO!" << endl;
100                 cout << "At least both of" << "<" << i + 1 << "," << j + 1 << ">" << "and" << "<" << j + 1 << "," << i + 1 << ">" << "appear together" << endl << endl << endl;
101                 return;
102             }
103         }
104     }
105     cout << "the answer is YES!" << endl << endl << endl;
106 }
107 void IsTransmit(int b[5][5], int n)
108 {
109     int i, j, k;
110     cout << "5、Is Transmit?" << endl << endl;
111     for (i = 0; i < n; i++)
112     {
113         for (j = 0; j < n; j++)
114         {
115             for (k = 0; k < n; k++)
116             {
117                 if (b[i][j] && b[j][k]&&!b[i][k])
118                 {
119                     
120                         cout << "the answer is NO!" << endl;
121                         cout << "At lease can't find" << "<" << i + 1 << "," << k + 1 << ">" << endl << endl << endl;
122                         return;
123                     
124                 }
125             
126             }
127         }
128     }
129     cout << "the answer is YES!" << endl << endl << endl;
130 }
131 int main()
132 {
133     int In[25][2];
134     int Pu[5][5] = {0};
135     int n ;
136 
137     cout << "input  number of the element(0<=n<=5)" << endl; //输入元素个数
138     
139     cin >> n;
140     for (int l = 0; l < n; l++)
141     {
142         cout << l + 1 <<" ";
143 
144     }
145     cout << endl;
146     while (n < 0||n>5)
147     {
148         cout << "error!" << endl;
149         cin >> n;
150     }
151     int s;
152     cout << "input a number of relation" << endl;//输入序偶个数
153     cin >> s;
154     cout << "input the relation" << endl;
155     Input(In, s);
156     Output(In,Pu, n,s);
157     IsReflexive(Pu, n);
158     IsAntireflexive(Pu,n);
159     IsSymmetric(Pu, n);
160     IsAntisymmetry(Pu, n);
161     IsTransmit(Pu, n);
162     system("pause");
163     return 0;
164 
165 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM