//All rights reserved by Liu Huan; Source from:https://doi.org/10.58473/JQPMC0014 #include #include #include #include #include using namespace std; const double E=0.005, F=0.003; vector>> generateArray(int I, int J, int K, double probability) { random_device rd; mt19937 gen(rd()); uniform_real_distribution<> dis(0.0, 1.0); vector>> arr(I, vector>(J, vector(K))); for (int i = 0; i < I; i++) { for (int j = 0; j < J; j++) { for (int k = 0; k < K; k++) { arr[i][j][k] = (dis(gen) < probability) ? 1 : 0; } } } return arr; } int countOnes(const vector>>& arr) { int count = 0; for (const auto& layer : arr) { for (const auto& row : layer) { for (int val : row) { if (val == 1) count++; } } } return count; } void printArray(const vector>>& arr) { for (int i = 0; i < arr.size(); i++) { cout << "Layer " << i << ":" << endl; for (int j = 0; j < arr[0].size(); j++) { for (int k = 0; k < arr[0][0].size(); k++) { cout << arr[i][j][k] << " "; } cout << endl; } cout << endl; } } int main() { double Density1, Density2, P1, P2; int I, J, K, T; cout << "To input the decimal values (0.00) of Density1 and Density2 (leaving a space in the middle): "; cin >> Density1 >> Density2; cout << "To input the integral values of I, J, K (leaving a space among values): "; cin >> I >> J >> K; cout << "To input the time intervals T: "; cin >> T; cout << "To input the value of P1 and P2 (leaving a space in the middle): "; cin >> P1 >> P2; vector>> final_c; int total_Reaction = 0; double final_Density1 = Density1, final_Density2 = Density2; for (int iter = 0; iter < T; iter++) { auto a = generateArray(I, J, K, P1 * Density1); auto b = generateArray(I, J, K, P2 * Density2); vector>> c(I, vector>(J, vector(K))); for (int i = 0; i < I; i++) { for (int j = 0; j < J; j++) { for (int k = 0; k < K; k++) { c[i][j][k] = a[i][j][k] * b[i][j][k]; } } } int Reaction = countOnes(c); total_Reaction += Reaction; Density1 = std::max(0.0, Density1 - E * Reaction); Density2 = std::max(0.0, Density2 - F * Reaction); if (iter == T - 1) { final_c = c; final_Density1 = Density1; final_Density2 = Density2; } } cout << "\nThe " << T << " Times cycle:" << endl; cout << "c[i][j][k]:" << endl; printArray(final_c); cout << "Total products (from the first time interval to the " << T << " times time interval): " << total_Reaction << endl; cout << "Updated Density1: " << fixed << setprecision(2) << final_Density1 << endl; cout << "Updated Density2: " << fixed << setprecision(2) << final_Density2 << endl; return 0; }