c++ 离散数学 群的相关判断及求解
采用C/C++/其它语言编程,构造一个n阶群<G={a,b,c,…},*>,G的阶|G|满足:3<=|G|<=6
1、判断该群是否是循环群,若是,输出该群的某个生成元。
2、给出每一个元素的阶和逆元
1 #include<iostream> 2 3 using namespace std; 4 //返回元素下标 5 //n为集合元素个数,set[]为存放元素集合,s为要求下标的元素 6 int sub(int n, char set[], char s) 7 { 8 for (int i = 0; i < n; i++) 9 { 10 if (set[i] == s) 11 return i; 12 } 13 } 14 15 //判断群是不是循环群 16 //n为群的阶数,,set[]为存放集合元素的数组,ele[6][6]为存放群运算表的二维数组 17 int Iscyc(int n, char set[], char ele[][6]) 18 { 19 int x, count; 20 int cnt; 21 for (int i = 0; i < n; i++) 22 { 23 count = 0;//计量---集合中的元素是否由set[i]的幂组成 24 25 for (int j = 0; j < n; j++) 26 { 27 x = i; 28 cnt = 2;//防止死循环 29 if (set[j] == set[i]) 30 count++; 31 if (set[j] == ele[x][i]) 32 count++; 33 34 while (set[j] != ele[x][i] && cnt < n) 35 { 36 x = sub(n, set, ele[x][i]); 37 cnt++; 38 if (set[j] == ele[x][i]) 39 count++; 40 } 41 42 43 } 44 if (count == n) 45 return 1; 46 } 47 return 0; 48 49 } 50 51 //输出循环群的生成元 52 //n为群的阶数,,set[]为存放集合元素的数组,ele[6][6]为存放群运算表的二维数组 53 void gen(int n, char set[], char ele[][6]) 54 { 55 int x, count; 56 int cnt; 57 for (int i = 0; i < n; i++) 58 { 59 count = 0; 60 for (int j = 0; j < n; j++) 61 { 62 x = i; 63 cnt = 2; 64 if (set[j] == set[i]) 65 count++; 66 if (set[j] == ele[x][i]) 67 count++; 68 while (set[j] != ele[x][i] && cnt < n) 69 { 70 x = sub(n, set, ele[x][i]); 71 cnt++; 72 if (set[j] == ele[x][i]) 73 count++; 74 } 75 } 76 if (count == n) 77 cout << "循环群的生成元是:" << set[i] << endl; 78 } 79 } 80 81 //给出群中每个元素的阶 82 //n为群的阶数,,set[]为存放集合元素的数组,ele[6][6]为存放群运算表的二维数组,ie为幺元 83 void order(int n, char set[], char ele[][6], char ie) 84 { 85 int x, cnt; 86 for (int i = 0; i < n; i++) 87 { 88 if (set[i] != ie) 89 { 90 x = i; 91 cnt = 2; 92 if (ele[x][i] == ie) 93 cout << set[i] << "的阶是" << cnt << endl; 94 while (ele[x][i] != ie) 95 { 96 x = sub(n, set, ele[x][i]); 97 cnt++; 98 if (ele[x][i] == ie) 99 { 100 cout << set[i] << "的阶是" << cnt << endl; 101 break; 102 } 103 } 104 } 105 else 106 cout << set[i] << "的阶是1" << endl; 107 } 108 109 110 } 111 112 //输出群中每个元素的逆元 113 //n为群的阶数,,set[]为存放集合元素的数组,ele[6][6]为存放群运算表的二维数组 114 //ie为幺元 115 void inver(int n, char set[], char ele[][6], char ie) 116 { 117 118 for (int i = 0; i < n; i++) 119 for (int j = 0; j < n; j++) 120 { 121 if (ele[i][j] == ie) 122 { 123 cout << set[i] << "的逆元是" << set[j] << endl; 124 break; 125 } 126 } 127 128 129 } 130 131 132 int main() 133 { 134 cout << "请输入元素的个数(要求3到6之间)" << endl; 135 int inputnum; 136 cin >> inputnum; 137 cout << "请输入元素" << endl; 138 char Y[4]; 139 for (int i = 0; i < inputnum; i++) 140 { 141 cin >> Y[i]; 142 } 143 cout << "请输入群的幺元" << endl; 144 char e; 145 cin >> e; 146 char outputform[7][7]; 147 char out1[6][6]; 148 outputform[0][0] = '*'; 149 for (int i = 1; i <= inputnum; i++) 150 { 151 outputform[0][i] = Y[i - 1]; 152 outputform[i][0] = Y[i - 1]; 153 } 154 int m; 155 for (int i = 0; i < inputnum; i++) 156 { 157 if (Y[i] == e) 158 { 159 m = i; 160 break; 161 } 162 } 163 int n = m; 164 int r = m; 165 for (int i = 0; i < inputnum; i++) 166 { 167 out1[n%inputnum][m] = Y[n%inputnum]; 168 n = n + 1; 169 } 170 int q; 171 for (int i = 0; i < inputnum; i++) 172 { 173 q = 0; 174 for (int y = 0; y < inputnum; y++) 175 { 176 if (out1[i][m] == Y[y]) 177 { 178 q = y; 179 break; 180 } 181 } 182 for (int j = m; j < inputnum + m; j++) 183 { 184 out1[i][j%inputnum] = Y[q%inputnum]; 185 q++; 186 } 187 } 188 for (int i = 1; i <= inputnum; i++) 189 { 190 for (int j = 1; j <= inputnum; j++) 191 { 192 outputform[i][j] = out1[i - 1][j - 1]; 193 } 194 } 195 for (int i = 0; i <= inputnum; i++) 196 { 197 for (int j = 0; j <= inputnum; j++) 198 { 199 cout << outputform[i][j] << ' '; 200 } 201 cout << endl; 202 } 203 204 205 cout << "---------------------------" << endl; 206 if (Iscyc(inputnum, Y, out1)) 207 { 208 cout << "<G,*>是循环群" << endl; 209 gen(inputnum, Y, out1); 210 } 211 else 212 cout << "<G,*>不是循环群" << endl; 213 cout << "---------------------------" << endl; 214 order(inputnum, Y, out1, e); 215 cout << "---------------------------" << endl; 216 inver(inputnum, Y, out1, e); 217 218 219 return 0; 220 }
努力,生活总会有改变