#include #include #include std::vector> getSmallestMatrix(const std::vector>& matrix, const std::vector& extraNums) { std::vector combined; // Flatten the 3x3 matrix into a 1D vector for (const auto& row : matrix) { for (int num : row) { combined.push_back(num); } } // Add extra numbers to the list combined.insert(combined.end(), extraNums.begin(), extraNums.end()); // Sort the combined numbers std::sort(combined.begin(), combined.end()); // Take the smallest 9 elements and form a new 3x3 matrix std::vector> newMatrix(3, std::vector(3)); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) newMatrix[i][j] = combined[i * 3 + j]; return newMatrix; } int main() { // Input 3x3 matrix std::vector> matrix = { {2, 4, 6}, {8, 10, 12}, {14, 16, 18} }; // Input additional numbers (5 numbers: 1, 3, 5, 7, 9) std::vector extraNums = {1, 3, 5, 7, 9}; // Get new matrix with the smallest numbers std::vector> newMatrix = getSmallestMatrix(matrix, extraNums); // Output the new matrix for (const auto& row : newMatrix) { for (int num : row) { std::cout << num << " "; } std::cout << std::endl; } return 0; }