离散实验程序代码3
1 //A,B,C的排列 2 #include <iostream> 3 using namespace std; 4 void main(void){ 5 char k=65; 6 int line,n=1; 7 cout<<"请输入你所需要的行数:"; 8 cin>>line; 9 10 for(int a=1 ;a<=line;a++) 11 { 12 for(int b=1 ;b<=2*n-1;b++) 13 {cout<<k;} 14 15 16 k++; 17 n++; 18 cout<<endl; 19 } 20 21 }
1 //离散实验3作业源程序.cpp 2 #include<iostream> 3 using namespace std; 4 5 int main() 6 { 7 int m, n, i, j, k; 8 cout << "Please input m and n:" << endl; 9 cin >> m >> n; 10 11 int *arr = new int[n]; 12 int **arr1 = new int *[n]; 13 for (i = 0; i < n; i++) 14 arr1[i] = new int[n]; 15 int *arr2 = new int[m]; 16 int **arr3 = new int*[m]; 17 for (i = 0; i < m; i++) 18 arr3[i] = new int[m]; 19 int **arr4 = new int*[n]; 20 for (i = 0; i < n; i++) 21 arr4[i] = new int[n]; 22 23 cout << "Please input " << n << " numbers:" << endl; 24 for (i = 0; i < n; i++) 25 cin >> arr[i]; 26 cout << "Please input " << n << " numbers:" << endl; 27 for (j = 0; j < m; j++) 28 cin >> arr2[j]; 29 30 for (i = 0; i < n; i++) 31 for (j = 0; j < n; j++) 32 { 33 if (arr[i] > arr[j]) 34 arr1[i][j] = 1; 35 else 36 arr1[i][j] = 0; 37 } 38 for (i = 0; i < m; i++) 39 for (j = 0; j < m; j++) 40 { 41 if (arr2[i] > arr2[j]) 42 arr3[i][j] = 1; 43 else 44 arr3[i][j] = 0; 45 } 46 cout << "矩阵arr1的逆矩阵为:" << endl; 47 for (i = 0; i < n; i++) 48 { 49 for (j = 0; j < n; j++) 50 { 51 cout << arr1[j][i] << " "; 52 } 53 cout << endl; 54 } 55 cout << "矩阵arr3的逆矩阵为:" << endl; 56 for (i = 0; i < n; i++) 57 { 58 for (j = 0; j < n; j++) 59 { 60 cout << arr3[j][i] << " "; 61 } 62 cout << endl; 63 } 64 65 cout << "矩阵arr1和arr3的布尔乘积为:" << endl; 66 for (i = 0; i < n; i++) 67 for (j = 0; j < n; j++) 68 for (k = 0; k < m; k++) 69 { 70 if (arr1[i][k] * arr3[k][j] == 1) 71 arr4[i][j] = 1; 72 else 73 arr4[i][j] = 0; 74 } 75 for (i = 0; i < n; i++) 76 { 77 cout << endl; 78 for (j = 0; j < m; j++) 79 cout << arr4[i][j] << " "; 80 } 81 cout << endl; 82 cout << "矩阵arr1 Warshall算法实现程序:" << endl; 83 84 for (k = 0; k <n; k++) 85 for (i =0; i < n; i++) 86 { 87 if (arr1[i][k] == 1) 88 for (j = 0; j <m; j++) 89 { 90 if (arr1[k][j] == 1) 91 arr1[i][j] = 1; 92 } 93 } 94 95 for (k = 0; k < n; k++) 96 for (i = 0; i < n; i++) 97 { 98 if (arr3[i][k] == 1) 99 for (j = 0; j < m; j++) 100 { 101 if (arr3[k][j] == 1) 102 arr3[i][j] = 1; 103 } 104 } 105 106 107 for (i = 0; i < n; i++) 108 { 109 for (int j = 0; j < n; j++) 110 cout << arr1[i][j] << " "; 111 cout << endl; 112 } 113 cout << endl; 114 for (i = 0; i < n; i++) 115 { 116 for (int j = 0; j < n; j++) 117 cout << arr3[i][j] << " "; 118 cout << endl; 119 } 120 }
1 //参考代码.cpp 2 #include<iostream> 3 4 using namespace std; 5 void strMatrix(int *a1, int **a2, int n); 6 void output(int **a2, int n); 7 int main() 8 { 9 int n, i, c, d; 10 cout << "How many numbers do you want to input:" << endl; 11 cin >> n; 12 int *arr1 = new int[n]; 13 int **arr2 = new int*[n]; 14 for (i = 0; i < n; i++) 15 arr2[i] = new int[n]; 16 cout << "Please input the numbers:" << endl; 17 for (i = 0; i < n; i++) 18 cin >> arr1[i]; 19 cout << "Please input the ralations:" << endl; 20 cin >> c; 21 cin >> d; 22 strMatrix(arr1, arr2, n); 23 output(arr2, n); 24 } 25 void strMatrix(int *a1, int **a2, int n) 26 { 27 for (int i = 0; i < n; i++) 28 for (int j = 0; j < n; j++) 29 { 30 if (a1[i] % a1[j] == 0) 31 a2[i][j] = 1; 32 else 33 a2[i][j] = 0; 34 } 35 } 36 void output(int **a2, int n) 37 { 38 for (int i = 0; i < n; i++) 39 { 40 for (int j = 0; j < n; j++) 41 cout << a2[i][j] << " "; 42 cout << endl; 43 } 44 } 45 46 void zfx(int a[M][M], int n) 47 { 48 int i = 0, vale1 = 1, vale2 = 1;/*vale1确定关系的自反性,vale2 49 判断关系的反自反性*/ 50 while (i < n) 51 { 52 if (a[i][i] == 1) vale2 = 0; 53 else vale1 = 0; 54 i++; 55 } 56 if (vale1 == 1) printf("zfx\n"); 57 if (vale2 == 1) printf("fzfx\n"); 58 if (!(vale1) && !(vale2)) printf("wzfxywfzfx\n"); 59 } 60 void dcx(int a[M][M], int n) 61 { 62 int i, j, vale3 = 1, vale4 = 1;/*vale3确定关系的对称性,vale4 63 判断关系的反对称性*/ 64 for (i = 0; i < n; i++) 65 for (j = 0; j < i; j++) 66 if (a[i][j] == a[j][i]) 67 vale4 = 0; 68 else vale3 = 0; 69 if (vale3 == 1) printf("dcx\n"); 70 if (vale4 == 1) printf("fdcx\n"); 71 if (!(vale3) && !(vale4)) printf("wdcx wfdcx\n"); 72 } void cdx1(int a[M][M], int n)/*用MM ⊆2 算法来判断传递 性*/ 73 { 74 int b[M][M], i, j, k, vale = 1; 75 for (i = 0; i < n; i++) 76 for (j = 0; j < n; j++) 77 { 78 b[i][j] = 0; 79 for (k = 0; k < n; k++) 80 b[i][j] += a[i][k] * a[k][j];/*计算 2M */ 81 } 82 for (i = 0; i < n; i++) 83 for (j = 0; j < n; j++) 84 if (b[i][j] != 0) 85 b[i][j] = 1; 86 for (i = 0; i < n; i++) 87 for (j = 0; j < n; j++) 88 if (b[i][j] == 1) 89 if (a[i][j] != b[i][j]) 90 vale = 0; 91 if (vale) 92 printf("ycdx\n"); 93 else 94 printf("wcdx\n"); 95 } 96 void cdx2(int a[M][M], int n)/*用warshall算法来求传递闭 97 包数组b */ 98 { 99 int b[M][M], i, j, k, vale = 1; 100 for (i = 0; i < n; i++) 101 for (j = 0; j < n; j++) 102 b{ [i][j] = a[i][j]; 103 for (k = 0; k < n; k++) 104 i{ (f a[i][j] && (a[i][k] || a[j][k])) 105 b[i][k] = 1; 106 } 107 } 108 for (i = 0; i < n; i++) /* 判断数组a和b相等 */ 109 for (j = 0; j < n; j++) 110 (iaf[i][j] != b[i][j]) 111 { 112 vale = 0; break; 113 } 114 if (vale == 1) printf("cdx\n"); 115 else printf("wcdx\n"); 116 }